简体   繁体   中英

Entity Framework Update Not Saving

using .net 4.5.2, MVC5, Entity framework 6 and visual studio 2015.

I have a repository pattern set up with Ninject as my DI here is the common file.

 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;

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. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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