首页 > Web开发 > 详细

.net entity framework 泛型 更新与增加记录

时间:2014-01-28 18:34:54      阅读:433      评论:0      收藏:0      [点我收藏+]

 

 

bubuko.com,布布扣
static public bool SaveOrUpdate<T>(T entity) where T: class
        {
            bool result = false;
            using (wechatEntities context = new wechatEntities())
            {
                Type t = entity.GetType();  
                PropertyInfo[] properts = t.GetProperties();  
                PropertyInfo p = t.GetProperty("Id");
                if (p == null)
                    p = t.GetProperty("id");
                if (p == null)
                    p = t.GetProperty("ID");
                if (p == null)
                {
                    return result;
                }
                var id = Convert.ToInt32(p.GetValue(entity,null));
                

                if (id == 0)
                {
                    context.Set(entity.GetType()).Add(entity);
                }
                else
                {
                    context.Entry(entity).State = System.Data.EntityState.Modified;
                }
                result = true;
                context.SaveChanges();
            }
            return result; 
        }
bubuko.com,布布扣

.net entity framework 泛型 更新与增加记录

原文:http://www.cnblogs.com/zyip/p/3535376.html

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