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

利用反射更新类

时间:2017-04-20 10:24:01      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:tty   color   value   更新   each   .property   save   stat   span   

利用反射更新类

      #region 利用反射更新类
        /// <summary>
        /// 利用反射更新类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="entity"></param>
        /// <param name="db"></param>
        public static void UpdateModel<T>(T entity, DbContext db) where T : class
        {
            db.Set<T>().Attach(entity);
            foreach (System.Reflection.PropertyInfo p in entity.GetType().GetProperties())
            {
                //if (p.GetValue(entity) != null&&p.GetValue(entity).ToString()!= "&nbsp;")
                if (p.GetValue(entity) != null)
                {
                    db.Entry(entity).Property(p.Name).IsModified = true;
                }
            }
            db.SaveChanges();
        }
        #endregion

 

利用反射更新类

标签:tty   color   value   更新   each   .property   save   stat   span   

原文地址:http://www.cnblogs.com/yechangzhong-826217795/p/6737016.html

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