Entity Framework 5 Updating a Record

前端 未结 8 1144
悲哀的现实
悲哀的现实 2020-11-22 08:06

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

相关标签:
8条回答
  • 2020-11-22 08:39

    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.

    0 讨论(0)
  • Just to add to the list of options. You can also grab the object from the database, and use an auto mapping tool like Auto Mapper to update the parts of the record you want to change..

    0 讨论(0)
  • 2020-11-22 08:45
    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();
    
    0 讨论(0)
  • 2020-11-22 08:48

    I have added an extra update method onto my repository base class that's similar to the update method generated by Scaffolding. Instead of setting the entire object to "modified", it sets a set of individual properties. (T is a class generic parameter.)

    public void Update(T obj, params Expression<Func<T, object>>[] propertiesToUpdate)
    {
        Context.Set<T>().Attach(obj);
    
        foreach (var p in propertiesToUpdate)
        {
            Context.Entry(obj).Property(p).IsModified = true;
        }
    }
    

    And then to call, for example:

    public void UpdatePasswordAndEmail(long userId, string password, string email)
    {
        var user = new User {UserId = userId, Password = password, Email = email};
    
        Update(user, u => u.Password, u => u.Email);
    
        Save();
    }
    

    I like one trip to the database. Its probably better to do this with view models, though, in order to avoid repeating sets of properties. I haven't done that yet because I don't know how to avoid bringing the validation messages on my view model validators into my domain project.

    0 讨论(0)
  • 2020-11-22 08:50
    public interface IRepository
    {
        void Update<T>(T obj, params Expression<Func<T, object>>[] propertiesToUpdate) where T : class;
    }
    
    public class Repository : DbContext, IRepository
    {
        public void Update<T>(T obj, params Expression<Func<T, object>>[] propertiesToUpdate) where T : class
        {
            Set<T>().Attach(obj);
            propertiesToUpdate.ToList().ForEach(p => Entry(obj).Property(p).IsModified = true);
            SaveChanges();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:53

    You are looking for:

    db.Users.Attach(updatedUser);
    var entry = db.Entry(updatedUser);
    entry.Property(e => e.Email).IsModified = true;
    // other changed properties
    db.SaveChanges();
    
    0 讨论(0)
提交回复
热议问题