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("不能找到主键!");
            }

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

所有方法封装:

 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. #region 更新实体  
    2.        public bool Update<T>(T entity, string PrimaryKey, object PrimaryKeyValue) where T : class  
    3.        {  
    4.   
    5.   
    6.   
    7.   
    8.            Type type = typeof(T);  
    9.            string strName = entities.Connection.ConnectionString.Replace("name=", "");  
    10.            EntityKey key = null;  
    11.            try  
    12.            {  
    13.                key = entities.CreateEntityKey(type.Name, entity);  
    14.            }  
    15.            catch (Exception ex)  
    16.            {  
    17.                throw new Exception("不能找到主键!");  
    18.            }  
    19.            object propertyValue = null;  
    20.            T entityFromDB = (T)entities.GetObjectByKey(key);  
    21.   
    22.            if (null == entityFromDB)  
    23.                return false;  
    24.            PropertyInfo[] properties1 = entityFromDB.GetType().GetProperties();  
    25.            foreach (PropertyInfo property in properties1)  
    26.            {  
    27.                propertyValue = null;  
    28.                if (null != property.GetSetMethod())  
    29.                {  
    30.                    PropertyInfo entityProperty =  
    31.                          entity.GetType().GetProperty(property.Name);  
    32.                    if (entityProperty.PropertyType.BaseType ==  
    33.                        Type.GetType("System.ValueType") ||  
    34.                        entityProperty.PropertyType ==  
    35.                        Type.GetType("System.String"))  
    36.   
    37.                        propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);  
    38.                    if (propertyValue == null)  
    39.                    {  
    40.                        Thread.Sleep(50);  
    41.                        propertyValue = entity.GetType().GetProperty(property.Name).GetValue(entity, null);  
    42.                    }  
    43.                    if (null != propertyValue)  
    44.                    {  
    45.                        try  
    46.                        {  
    47.                            string Name = property.Name;// "Reference";  
    48.                            if (Name.IndexOf("Reference") < 0)  
    49.                            {  
    50.                                property.SetValue(entityFromDB, propertyValue, null);  
    51.                            }  
    52.                        }  
    53.                        catch (Exception ex) { }  
    54.                    }  
    55.                }  
    56.            }  
    57.   
    58.   
    59.            entities.SaveChanges();  
    60.            return true;  
    61.   
    62.        }  
    63.  
    64.       
    65.        #endregion  
posted @ 2014-12-30 11:59  关中秦人  阅读(486)  评论(0编辑  收藏  举报