简体   繁体   English

实体框架更新未保存

[英]Entity Framework Update Not Saving

using .net 4.5.2, MVC5, Entity framework 6 and visual studio 2015. 使用.net 4.5.2,MVC5,实体框架6和Visual Studio 2015。

I have a repository pattern set up with Ninject as my DI here is the common file. 我有一个使用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();
    }  

my Context 我的背景

 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();
}

Then my Repository update method. 然后是我的存储库更新方法。

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;
    }

Now when i save via my controller and call the repository update method, nothing is updated. 现在,当我通过控制器保存并调用存储库更新方法时,没有任何更新。

So i use 所以我用

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

But i get an error, 但是我得到一个错误

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. 附加信息:附加类型为'myapp.Models.UserBlueRayList'的实体失败,因为相同类型的另一个实体已经具有相同的主键值。 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 如果图形中的任何实体具有相互冲突的键值,则使用“附加”方法或将实体的状态设置为“不变”或“修改”时,可能会发生这种情况。

Many to many models, userlist model. 多对多模型,用户列表模型。

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; }
}

And the

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; }
}

Question is why this wont update, and why it errors if i try to set state to modified. 问题是为什么这将不会更新,并且如果我尝试将状态设置为修改,为什么会出错。

Since you are using EF6, you may try to use auto property mapping built into EF6 由于您使用的是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;
    }

Cheers 干杯

First Solution you have to update like that 您必须像这样更新的第一个解决方案

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

        userBRList.Name = item.Name;
         //value assign to other entity
        _db.Entry(userBRList).State = EntityState.Modified;
        _db.SaveChanges();
        return true;
    }
    return false;
}

if this will not solve your problem you Find method instead of FirstorDefault() 如果这不能解决您的问题,请使用Find方法代替FirstorDefault()

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

    userBRList.Name = item.Name;
     //value assign to other entity
    _db.Entry(userBRList).State = EntityState.Modified;
    _db.SaveChanges();
    return true;
}
return false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM