c# - Entity Framework 更新未保存

标签 c# asp.net-mvc entity-framework asp.net-mvc-5

使用.net 4.5.2、MVC5、Entity Framework 6 和 Visual Studio 2015。

我使用 Ninject 设置了一个存储库模式,因为这里的 DI 是通用文件。

 private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ApplicationDbContext>().ToSelf().InRequestScope();
        kernel.Bind<IUserBlueRayLists>().To<UserBlueRayListRepository>().InRequestScope();
        kernel.Bind<IBlueRays>().To<BlueRaysRepository>().InRequestScope();
    }  

我的背景

 public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public IDbSet<UserBlueRayList> UserBlueRayLists { get; set; }
    public IDbSet<BlueRays> BlueRays { get; set; }

    public new void SaveChanges()
    {
        base.SaveChanges();
    }
}

public interface IDevTestContext
{
    IDbSet<UserBlueRayList> UserBlueRayLists { get; set; }
    IDbSet<BlueRays> BlueRays { get; set; }

    void SaveChanges();
}

然后是我的存储库更新方法。

public bool Update(UserBlueRayList item)
    {
        var userBRList = _db.UserBlueRayLists.FirstOrDefault(x => x.Id == item.Id);
        if(userBRList != null)
        {

            userBRList = item;
            //_db.Entry(userBRList).State = EntityState.Modified;
            _db.SaveChanges();
            return true;
        }
        return false;
    }

现在,当我通过 Controller 保存并调用存储库更新方法时,没有任何更新。

所以我使用

_db.Entry(userBRList).State = EntityState.Modified;

但是我收到错误,

Additional information: Attaching an entity of type 'myapp.Models.UserBlueRayList' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values... etc

多对多模型,用户列表模型。

public class UserBlueRayList
{
    public UserBlueRayList()
    {
        this.BlueRays = new HashSet<BlueRays>();
    }

    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Description { get; set; }

    [Required]
    public string UserId { get; set; }

    public virtual ICollection<BlueRays> BlueRays { get; set; }
}

还有

public class BlueRays
{
    public BlueRays()
    {
        this.UserBlueRayList = new HashSet<UserBlueRayList>();
    }

    [Key]
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }

    [Required]
    public string Description { get; set; }


    public virtual ICollection<UserBlueRayList> UserBlueRayList { get; set; }
}

问题是为什么这不会更新,以及为什么如果我尝试将状态设置为已修改会出错。

最佳答案

由于您使用的是 EF6,因此您可以尝试使用 EF6 内置的自动属性映射

public bool Update(UserBlueRayList item)
    {
        var userBRList = _db.UserBlueRayLists.FirstOrDefault(x => x.Id == item.Id);
        if(userBRList != null)
        {
            _dbContext.Entry(userBRList).CurrentValues.SetValues(item); 
            return return _dbContext.SaveChanges() > 0;
        }
        return false;
    }

干杯

关于c# - Entity Framework 更新未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42195542/

相关文章:

c# - 调用 Google Cloud Speech API 未返回任何内容,10 分钟后失败

c# - 如何在 Node.js WebSocket 服务器和 C# 客户端之间交换二进制数据?

asp.net - 尝试通过方法 'HttpConfiguration..ctor(HttpRouteCollection)' 访问方法 'HttpConfiguration.DefaultFormatters()' 失败

c# - EF Code First 处理多对多关系

c# - 如何在 Entity Framework 中获取数字字符串列的最大值

c# - 使用 DotNetOpenAuth 实现 API key

c# - ASP.NET Web Api 依赖注入(inject)——单例与否

asp.net-mvc - ASP.NET MVC - 通过 GET 提交时让 Html.BeginForm() 记住查询字符串参数

jquery - KnockOutJs 对象未发布

c# - 角色管理器是否为自定义角色提供程序缓存数据,我可以清除此缓存吗?