C# 使用实体框架更新对象的所有字段

C# 使用实体框架更新对象的所有字段,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我想使用实体框架更改对象的所有属性。 搜索之后,我得到了这个: 控制器,动作: public ActionResult Test() { var user = GetCurrentUser(); user.FirstName = "BLAH BLAH"; new UserRepository().UpdateUser(user); return RedirectToAction("Index"); } 在我的用户存储库中: public bool Update

我想使用实体框架更改对象的所有属性。
搜索之后,我得到了这个:

控制器,动作:

public ActionResult Test()
{
    var user = GetCurrentUser();
    user.FirstName = "BLAH BLAH";
    new UserRepository().UpdateUser(user);
    return RedirectToAction("Index");
}
在我的用户存储库中:

public bool UpdateUser(ApplicationUser target)
{
     using (var db = new AppDatabase())
     {
        db.Entry(target).State = EntityState.Modified;
        db.SaveChanges();
        return true;
     }
}
但是当我尝试执行时,我得到了这个错误

EntityChangeTracker的多个实例不能引用实体对象

那么,有什么方法可以修复或者更好的方法吗?
使用实体框架6.0.0和.net 4.5

public ApplicationUser GetCurrentUser()
{
   return UserManager.FindById(User.Identity.GetUserId());
}

确保所有对象都来自同一上下文

var userContextOne = new MyDbContext();
var user = userContextOne.Users.FirstOrDefault();

var AppDbContextTwo = new MyDbContext();

// Warning when you work with entity properties here! Be sure that all objects came from the same context!
db.Entry(target).State = EntityState.Modified;

AppDbContextTwo.SaveChanges();
scond问题(与异常无关!):


你为什么这么做?!你没有分离的场景吗?您是否已禁用Changetracker?无论如何,只要执行DetectChanges,此方法将查找更改后的数据,您不必自己执行。

您应该使用相同的数据库上下文实例进行查找和更新,因此您可以:

 class UserRepository : IDisposable //using IDisposable to dispose db context
 {
      private AppDatabase _context;
      public UserRepository()
      {
           _context = new AppDatabase();
      }

      public ApplicationUser Find(string id)
      {
           return _context.Set<ApplicationUser>().Find(id);
      }

      public void Update(ApplicationUserentity entity)
      {
          _context.Entry(entity).State = EntityState.Modified;
          _context.SaveChanges();
      }

      public void Dispose()
      {
           _context.Dispose();
      }   
 }

我还认为使用依赖注入框架对您是有益的。所以去做吧

您在
Test
action中创建了多少数据库上下文?你能展示一下你对
GetCurrentUser
的方法吗?@nizzik one,添加了GetCurrentUser方法我只使用一个上下文。我所有的代码都在上面。第二个上下文来自usermanager吗?那么如何解决这个问题呢?
db.Entry(target).State=EntityState.Detached
似乎是合理的,并且
使用(var userContext=new EntityContext())
可能有助于防止重复或多个包含同一类实例的上下文。不,每次使用(var db=new AppDatabase()),用户作为参数形式在方法中的某处传递else@bassamAlugili非常感谢你的帮助,我找到了解决办法。
 class UserRepository : IDisposable //using IDisposable to dispose db context
 {
      private AppDatabase _context;
      public UserRepository()
      {
           _context = new AppDatabase();
      }

      public ApplicationUser Find(string id)
      {
           return _context.Set<ApplicationUser>().Find(id);
      }

      public void Update(ApplicationUserentity entity)
      {
          _context.Entry(entity).State = EntityState.Modified;
          _context.SaveChanges();
      }

      public void Dispose()
      {
           _context.Dispose();
      }   
 }
public ActionResult Test()
{
    using (var repository = new UserRepository())
    {
         var user = repository.Find(User.Identity.GetUserId());
         user.FirstName = "BLAH BLAH";
         repository.Update(user);
    }
    return RedirectToAction("Index");
}