Entity Framework 5 Updating a Record

前端 未结 8 1238
悲哀的现实
悲哀的现实 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:50

    public interface IRepository
    {
        void Update(T obj, params Expression>[] propertiesToUpdate) where T : class;
    }
    
    public class Repository : DbContext, IRepository
    {
        public void Update(T obj, params Expression>[] propertiesToUpdate) where T : class
        {
            Set().Attach(obj);
            propertiesToUpdate.ToList().ForEach(p => Entry(obj).Property(p).IsModified = true);
            SaveChanges();
        }
    }
    

提交回复
热议问题