EF5框架封装

         话说上周,在弄系统,因为是新电脑,就没有沿用以前的VS2010换了2013使用,然后因为更新了数据库表结构,于是对EF的生成的实体进行更新。然后手贱一点而过,结果发现底层运行不聊了。一看原因:AccessBase<T> where T : EntityObject 。

     是什么原因了,刚开始看到还是比较晕,这里没有问题啊,怎么会报错呢。然后查找源码发现,EF5 针对实体生成的是Class而非原来的EntityObject。
    public partial class SysOperateLog。 其实不晓得这是一种进步或是一种退步的方式。在Linq里面,微软就是根据class来进行相关的操作的。以前比较喜欢Linq,但是长时间用EF也用习惯了。针对以前的EF4的框架,现更新如下:

      public bool Update<T>(T entity, string PrimaryKey, object PrimaryKeyValue) where T : class
        {

      主要针对底层Update方法,因为以前entity:EntityObject是能通过entity 找到主键的,现在肯定是不行了。

            Type type = typeof(T);
            string strName = entities.Connection.ConnectionString.Replace("name=", "");
            EntityKey key = null;
            try
            {
                key = entities.CreateEntityKey(type.Name, entity);
            }
            catch (Exception ex)
            {
                throw new Exception("不能找到主键!");
            }

直接通过上面的方法找到主键即可。

所有方法封装:

 #region 更新实体
        public bool Update<T>(T entity, string PrimaryKey, object PrimaryKeyValue) where T : class
        {




            Type type = typeof(T);
            string strName = entities.Connection.ConnectionString.Replace("name=", "");
            EntityKey key = null;
            try
            {
                key = entities.CreateEntityKey(type.Name, entity);
            }
            catch (Exception ex)
            {
                throw new Exception("不能找到主键!");
            }
            object propertyValue = null;
            T entityFromDB = (T)entities.GetObjectByKey(key);

            if (null == entityFromDB)
                return false;
            PropertyInfo[] properties1 = entityFromDB.GetType().GetProperties();
            foreach (PropertyInfo property in properties1)
            {
                propertyValue = null;
                if (null != property.GetSetMethod())
                {
                    PropertyInfo entityProperty =
                          entity.GetType().GetProperty(property.Name);
                    if (entityProperty.PropertyType.BaseType ==
                        Type.GetType("System.ValueType") ||
                        entityProperty.PropertyType ==
                        Type.GetType("System.String"))

                        propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);
                    if (propertyValue == null)
                    {
                        Thread.Sleep(50);
                        propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);
                    }
                    if (null != propertyValue)
                    {
                        try
                        {
                            string Name = property.Name;// "Reference";
                            if (Name.IndexOf("Reference") < 0)
                            {
                                property.SetValue(entityFromDB, propertyValue, null);
                            }
                        }
                        catch (Exception ex) { }
                    }
                }
            }


            entities.SaveChanges();
            return true;

        }

      
        #endregion


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值