本文介绍了获得"实体类型<&型号GT;是不是该机型为当前上下文的一部分&QUOT。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题使用Web API中asp.net同时更新我的​​数据库1列。我想查询就把行中只更新,而不是更新一个,并设置其余为null一个值。我做了一个单独的模型控制器之外采取在更新,所以我可以做一次的。当我打这行 db.Entry(用户).STATE = EntityState.Modified; 的控制器,在那里示数出来。任何意见,我怎么能解决这个问题?

这是我独立的视图模型我在put方法采取在:

 命名空间WebAPI.Models.ViewModels
{
    公共类UserViewModel
    {
        公共字符串名字{获得;组; }
        公共字符串名字{获得;组; }
    }
}

这是我的控制器调用在我参数与视图模型方法:

 公开的Htt presponseMessage PutUser(INT ID,UserViewModel用户)
        {
            HTT presponseMessage响应;            如果(db.User.IsInRole(管理))
            {
                尝试
                {
                        db.Entry(用户).STATE = EntityState.Modified;
                        db.SaveChanges();
                }
                赶上(DbUpdateConcurrencyException)
                {
                    如果(!UserExists(ID))
                    {
                        响应=新的Htt presponseMessage(的HTTPStatus code.NotFound);
                        返回响应;
                    }
                    其他
                    {
                        扔;
                    }
                }                响应=新的Htt presponseMessage(的HTTPStatus code.NoContent);
                返回响应;
            }

这是我的的DbContext 文件:

 公共部分类实体:的DbContext
    {
        公共实体()
            :基地(NAME =实体)
        {
        }        保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
        {
            抛出新的意外codeFirstException();
        }
        公共虚拟DbSet<使用者>用户{搞定;组; }
    }
}


解决方案

这个错误来自于你如何初始化的数据上下文分贝

用户对象已经在一个单独的创建数据库,所以,当你试图更新用户,当前分贝不知道这个用户对象。

您可以通过让用户解决这个问题。

 尝试
{
    //或检查名字和姓氏的,如果你没有一个用户ID
    变种updatedUser = db.Users.SingleOrDefault(X => x.id == ID);    updatedUser.FirstName = user.FirstName;
    updatedUser.LastName = user.LastName;    db.Entry(updatedUser).STATE = EntityState.Modified;
    db.SaveChanges();
 }

此外,也可以确保数据上下文您使用创建用户对象是一样的一个是试图更新用户。

这是否对你有意义?

I am having this issue updating my database 1 column at a time in asp.net using web api. I am trying to query a PUT to just update one value in the row instead of updating that one and setting the rest to null. I made a separate model outside of the controller to take in the update so I could do one at a time. When I hit this line db.Entry(user).State = EntityState.Modified; in the controller that is where it is erroring out. Any advice how I can fix this?

This is my separate ViewModel I am taking in in the put method:

namespace WebAPI.Models.ViewModels
{
    public class UserViewModel
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

This is my controller calling in the method with the ViewModel in my parameter:

public HttpResponseMessage PutUser(int id, UserViewModel user)
        {
            HttpResponseMessage response;

            if (db.User.IsInRole("Admin"))
            {
                try
                {
                        db.Entry(user).State = EntityState.Modified;
                        db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!UserExists(id))
                    {
                        response = new HttpResponseMessage(HttpStatusCode.NotFound);
                        return response;
                    }
                    else
                    {
                        throw;
                    }
                }

                response = new HttpResponseMessage(HttpStatusCode.NoContent);
                return response;
            }

This is my DBContext file:

public partial class Entities : DbContext
    {
        public Entities()
            : base("name=Entities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }
        public virtual DbSet<User> Users { get; set; }
    }
}
解决方案

The error comes from how you initialize the data context db.

The user object has been created in a separate db, so, when you are trying to update user, the current db doesn't know about this user object.

You could solve it by getting a user

try
{
    // or check on FirstName and LastName if you don't have a user id
    var updatedUser = db.Users.SingleOrDefault(x => x.id == id);

    updatedUser.FirstName = user.FirstName;
    updatedUser.LastName = user.LastName;

    db.Entry(updatedUser).State = EntityState.Modified;
    db.SaveChanges();
 }

Alternatively, you could make sure that the data context you are using to create the user object is the same as the one that is trying to update the user.

Does this make sense to you?

这篇关于获得&QUOT;实体类型&lt;&型号GT;是不是该机型为当前上下文的一部分&QUOT。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:16