首页 > 其他 > 详细

直接使用提交过来的类来更新字段EntityState.Modified并过滤null值的方法

时间:2017-01-04 21:34:13      阅读:344      评论:0      收藏:0      [点我收藏+]
public T Update<T>(T entity) where T : ModelBase
        {
            var set = this.Set<T>();
            set.Attach(entity);
            foreach (System.Reflection.PropertyInfo p in entity.GetType().GetProperties())
            {
                if (p.GetValue(entity) != null)
                {
                    this.Entry<T>(entity).Property(p.Name).IsModified = true;
                }
            }
            this.SaveChanges();
            return entity;
        }

 

 foreach (PropertyInfo p in codeRuleEntity.GetType().GetProperties())
  {
    if (p.GetValue(codeRuleEntity) == null)
     {
          db.Entry(codeRuleEntity).Property(p.Name).IsModified = true;
     }
  }
db.Entry(codeRuleEntity).State = EntityState.Modified;

 

直接使用提交过来的类来更新字段EntityState.Modified并过滤null值的方法

原文:http://www.cnblogs.com/firstcsharp/p/6250151.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!