简体   繁体   English

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

[英]Update all fields of an object using entity framework

I want to change all of an object properties using entity framwork. 我想使用实体框架更改所有对象属性。
after searching i got to have this: 搜索后,我必须有这个:

Controller,action: 控制器,动作:

public ActionResult Test()
{
    var user = GetCurrentUser();
    user.FirstName = "BLAH BLAH";
    new UserRepository().UpdateUser(user);
    return RedirectToAction("Index");
}

and in my UserRepository: 在我的UserRepository中:

public bool UpdateUser(ApplicationUser target)
{
     using (var db = new AppDatabase())
     {
        db.Entry(target).State = EntityState.Modified;
        db.SaveChanges();
        return true;
     }
}

but when i try execute i got this error 但是当我尝试执行时出现此错误

An entity object cannot be referenced by multiple instances of EntityChangeTracker. EntityChangeTracker的多个实例不能引用一个实体对象。

so,any ways to fix or any better way? 那么,有什么方法可以修复还是有更好的方法?
using entity framework 6.0.0 and .net 4.5 使用实体框架6.0.0和.net 4.5

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

Be sure that all objects came from the same context! 确保所有对象都来自同一上下文!

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

The scond problem (not related to the exception!): 第二个问题(与异常无关!):

db.Entry(target).State = EntityState.Modified;

Why you are doing that?! 你为什么这样做? You dont not have Detached Scenario? 您没有独立场景? did you have disabled your Changetracker? 您是否禁用了Changetracker? anyway just execute DetectChanges and this method will find the changed data you do not have to do it by your self. 无论如何,只需执行DetectChanges,此方法将找到您不必自己进行更改的数据。

You should use same instance of db context for finding and updating, so you UserRepository can be: 您应该使用数据库上下文的同一实例进行查找和更新,因此UserRepository可以是:

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

You can use it in controller: 您可以在控制器中使用它:

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

I also think using some dependency injection framework would be beneficial for you. 我也认为使用某种依赖注入框架对您会有所帮助。 So go for it!! 所以去吧!

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

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