首页 > 其他 > 详细

利用反射更新类

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

利用反射更新类

      #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

 

利用反射更新类

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

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