/// <summary>
 /// EF的更新最好的办法就是先查询再修改,不然很麻烦
 /// </summary>
 /// <param name="obj"></param>
 /// <param name="getPkHandler"></param>
 public void Update(T obj, Func <T, object> getPkHandler)
 {
     using (var context = new ModernHisContext())
     {
         var key    = getPkHandler.Invoke(obj);
         var oldObj = context.Set <T>().Find(key);
         if (oldObj != null)
         {
             ObjectRefletUtil.SetValue <T>(oldObj, obj);
             context.Entry <T>(oldObj).State = EntityState.Modified;
             context.SaveChanges();
         }
     }
 }
Ejemplo n.º 2
0
        public virtual T Update(T obj)
        {
            var key    = ObjectRefletUtil.GetMainKeyValue(obj);
            var oldObj = context.Set <T>().Find(key);

            if (oldObj == null)
            {
                return(null);
            }
            ObjectRefletUtil.SetValue <T>(oldObj, obj);
            context.Entry <T>(oldObj).State = EntityState.Modified;
            context.SaveChanges();
            return(oldObj);
        }