码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-01-04 21:34:26      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:字段   div   更新   ase   提交   set   log   public   更新字段   

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值的方法

标签:字段   div   更新   ase   提交   set   log   public   更新字段   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!