Entity Framework 5 Updating a Record

I have been exploring different methods of editing/updating a record within Entity Framework 5 in an ASP.NET MVC3 environment, but so far none of them tick all of the boxes I need. I'll explain why.

I have found three methods to which I'll mention the pros and cons:

Method 1 - Load original record, update each property

var original = db.Users.Find(updatedUser.UserId);

if (original != null)
{
    original.BusinessEntityId = updatedUser.BusinessEntityId;
    original.Email = updatedUser.Email;
    original.EmployeeId = updatedUser.EmployeeId;
    original.Forename = updatedUser.Forename;
    original.Surname = updatedUser.Surname;
    original.Telephone = updatedUser.Telephone;
    original.Title = updatedUser.Title;
    original.Fax = updatedUser.Fax;
    original.ASPNetUserId = updatedUser.ASPNetUserId;
    db.SaveChanges();
}    

Pros

  • Can specify which properties change
  • Views don't need to contain every property
  • Cons

  • 2 x queries on database to load original then update it
  • Method 2 - Load original record, set changed values

    var original = db.Users.Find(updatedUser.UserId);
    
    if (original != null)
    {
        db.Entry(original).CurrentValues.SetValues(updatedUser);
        db.SaveChanges();
    }
    

    Pros

  • Only modified properties are sent to database
  • Cons

  • Views need to contain every property
  • 2 x queries on database to load original then update it
  • Method 3 - Attach updated record and set state to EntityState.Modified

    db.Users.Attach(updatedUser);
    db.Entry(updatedUser).State = EntityState.Modified;
    db.SaveChanges();
    

    Pros

  • 1 x query on database to update
  • Cons

  • Can't specify which properties change
  • Views must contain every property
  • Question

    My question to you guys; is there a clean way that I can achieve this set of goals?

  • Can specify which properties change
  • Views don't need to contain every property (such as password!)
  • 1 x query on database to update
  • I understand this is quite a minor thing to point out but I may be missing a simple solution to this. If not method one will prevail ;-)


    您正在寻找:

    db.Users.Attach(updatedUser);
    var entry = db.Entry(updatedUser);
    entry.Property(e => e.Email).IsModified = true;
    // other changed properties
    db.SaveChanges();
    

    I really like the accepted answer. I believe there is yet another way to approach this as well. Let's say you have a very short list of properties that you wouldn't want to ever include in a View, so when updating the entity, those would be omitted. Let's say that those two fields are Password and SSN.

    db.Users.Attach(updatedUser);
    
    var entry = db.Entry(updatedUser);
    entry.State = EntityState.Modified;
    
    entry.Property(e => e.Password).IsModified = false;
    entry.Property(e => e.SSN).IsModified = false;   
    
    db.SaveChanges();   
    

    This example allows you to essentially leave your business logic alone after adding a new field to your Users table and to your View.


    foreach(PropertyInfo propertyInfo in original.GetType().GetProperties()) {
        if (propertyInfo.GetValue(updatedUser, null) == null)
            propertyInfo.SetValue(updatedUser, propertyInfo.GetValue(original, null), null);
    }
    db.Entry(original).CurrentValues.SetValues(updatedUser);
    db.SaveChanges();
    
    链接地址: http://www.djcxy.com/p/5738.html

    上一篇: 如何更新实体框架中的相关数据6

    下一篇: 实体框架5更新记录