EF5框架封装
来源:互联网 发布:东华软件面试题答案 编辑:程序博客网 时间:2024/06/05 07:58
话说上周,在弄系统,因为是新电脑,就没有沿用以前的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
- EF5框架封装
- ExtJS4.1+MVC3+Spring.NET1.3+EF5 整合一:构建项目框架
- 一些框架封装
- MFC--框架封装
- ajax框架封装
- Extjs框架的封装
- 封装Ajax框架
- Android OkHttp封装 框架
- Volley框架二次封装
- okhttp框架的封装
- 封装javaScript框架思想
- 封装缓动框架
- 封装javaScript框架思想
- OkHttpClient网络框架封装
- 封装android http框架
- Retrofit框架基本封装
- 封装mvc框架
- 封装mvc框架【二】
- C++中的static关键字
- 如何高效率从整数中地取个位上的数,十位,百位
- 系统维护 Win7安装过程中创建扩展分区和逻辑分区的方法
- Dao工厂类代码实例
- 比较日期先后
- EF5框架封装
- Sphinx搜索引擎(1)——sphinx
- 高效的larbin
- eclipse常用整理
- Linux网络编程----IP转换函数--inet_addr和inet_ntoa
- 【ICE】ICE协议看明白
- 字符串的组合
- 解释性语言和编译性语言
- 公司请你来干嘛?当然不是发工资的