EF 5.0 帮助类

加入命名空间:

  1. using System;
  2. using System.Data;
  3. using System.Data.Entity;
  4. using System.Data.Entity.Infrastructure;
  5. using System.Linq;

接口:

  1. public interface IEFRepository<TEntity> where TEntity : class
  2. {
  3. bool AddEntity(TEntity entity);
  4. bool UpdateEntity(TEntity entity);
  5. bool UpdateEntity(IEnumerable<TEntity> entities);
  6. bool DeleteEntity(int ID);
  7. bool DeleteEntity(TEntity entity);
  8. bool DeleteEntity(Expression<Func<TEntity, bool>> predicate);
  9. bool DeleteEntity(IEnumerable<TEntity> entities);
  10. IList<TEntity> LoadEntities(Func<TEntity, bool> whereLambda);
  11. IList<TEntity> LoadEntities(int pageIndex = 1, int pageSize = 30, Func<TEntity, bool> whereLambda = null);
  12. TEntity FindByID(int ID);
  13. }

具体类:

  1. //EF5.0的写法
  2. public class EFRepository<TEntity> : IEFRepository<TEntity> where TEntity : class
  3. {
  4. #region 单利模式
  5. public static EFRepository<TEntity> Instance = new EFRepository<TEntity>();
  6. public EFRepository( )
  7. {
  8. Create();
  9. }
  10. #endregion
  11.  
  12. /// <summary>
  13. /// 获取 当前使用的数据访问上下文对象
  14. /// </summary>
  15. public DbContext Context
  16. {
  17. get
  18. {
  19. return EFDbContextHelper.Context;
  20. }
  21. }
  22. public bool AddEntity(TEntity entity)
  23. {
  24. EntityState state = Context.Entry(entity).State;
  25. if (state == EntityState.Detached)
  26. {
  27. Context.Entry(entity).State = EntityState.Added;
  28. }
  29. Context.SaveChanges();
  30. return true;
  31. }
  32. public bool UpdateEntity(TEntity entity)
  33. {
  34. Context.Set<TEntity>().Attach(entity);
  35. Context.Entry<TEntity>(entity).State = EntityState.Modified;
  36. return Context.SaveChanges() > 0;
  37. }
  38. public bool UpdateEntity(IEnumerable<TEntity> entities)
  39. {
  40. try
  41. {
  42. Context.Configuration.AutoDetectChangesEnabled = false;
  43. foreach (TEntity entity in entities)
  44. {
  45. UpdateEntity(entity);
  46. }
  47. return true;
  48. }
  49. finally
  50. {
  51. Context.Configuration.AutoDetectChangesEnabled = true;
  52. }
  53. }
  54. public bool DeleteEntity(int ID)
  55. {
  56. TEntity entity = FindByID(ID);
  57. return DeleteEntity(entity);
  58. }
  59. public bool DeleteEntity(TEntity entity)
  60. {
  61. Context.Set<TEntity>().Attach(entity);
  62. Context.Entry<TEntity>(entity).State = EntityState.Deleted;
  63. return Context.SaveChanges() > 0;
  64. }
  65. public bool DeleteEntity(System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate)
  66. {
  67. List<TEntity> entities = Set().Where(predicate).ToList();
  68. return Context.SaveChanges() > 0;
  69. }
  70. public bool DeleteEntity(IEnumerable<TEntity> entities)
  71. {
  72. try
  73. {
  74. Context.Configuration.AutoDetectChangesEnabled = false;
  75. foreach (TEntity entity in entities)
  76. {
  77. DeleteEntity(entity);
  78. }
  79. return true;
  80. }
  81. finally
  82. {
  83. Context.Configuration.AutoDetectChangesEnabled = true;
  84. }
  85. }
  86. public IList<TEntity> LoadEntities(Func<TEntity, bool> whereLambda)
  87. {
  88. if (whereLambda != null)
  89. return Context.Set<TEntity>().Where<TEntity>(whereLambda).AsQueryable().ToList();
  90. else
  91. return Context.Set<TEntity>().AsQueryable().ToList();
  92. }
  93. public IList<TEntity> LoadEntities(int pageIndex = 1, int pageSize = 30, Func<TEntity, bool> whereLambda = null)
  94. {
  95. int skinCount = (pageIndex - 1) * pageSize;
  96. if (whereLambda != null)
  97. return Set()
  98. .Where<TEntity>(whereLambda)
  99. .Skip(skinCount)
  100. .Take(pageSize)
  101. .ToList();
  102. else
  103. return Set()
  104. .Skip(skinCount)
  105. .Take(pageSize)
  106. .ToList();
  107. }
  108. public DbSet<TEntity> Set( )
  109. {
  110. return Context.Set<TEntity>();
  111. }
  112. public TEntity FindByID(int ID)
  113. {
  114. return Set().Find(ID);
  115. }
  116. private TEntity Create( )
  117. {
  118. return Context.Set<TEntity>().Create();
  119. }
  120. }

使用:

准备实体类

  1. /// <summary>
  2. /// 实体类楼层管理 。(属性说明自动提取数据库字段的描述信息)
  3. /// </summary>
  4. [Serializable]
  5. public class Floor
  6. {
  7. public int ID { get; set; }
  8. [Category("楼层名称")]
  9. public string f_Name { get; set; }
  10. [Category("备注")]
  11. public string f_Remark { get; set; }
  12. }

使用EF帮助类调用

  1. /// <summary>
  2. /// 数据上下文 Db3983Context
  3. /// </summary>
  4. public class Db3983Context : EFDbContext
  5. {
  6. /// <summary>
  7. /// 构造函数
  8. /// </summary>
  9. public Db3983Context()
  10. : base("3983")
  11. {
  12. }
  13. /// <summary>
  14. /// 楼层管理
  15. /// </summary>
  16. public DbSet<Floor> Floor { get; set; }
  17. }
  1. /// <summary>
  2. /// 应用程序的主入口点。
  3. /// </summary>
  4. [STAThread]
  5. static void Main( )
  6. {
  7. EFDbContextHelper.Context = new Db3983Context();
  8.  
  9. Floor floor = new Floor();
  10. floor.f_Name = "罗敏贵";
  11. floor.f_Remark = "我这个人看上去很靠谱,长得也很高有一米八五,也很帅气,千万不要迷恋哥,哥只是传说。";
  12.  
  13. EFRepository<Floor>.Instance.AddEntity(floor);
  14. }

扩展:

其他ORM只要现实上面的接口就可以了,然后在配置文件制定使用哪个ORM就可以做到扩展了。

http://www.cnblogs.com/lori/archive/2012/10/19/2731801.html

EF 5.0 帮助类的更多相关文章

  1. EF 5.0 帮助类 增删改查

    原文地址:http://www.cnblogs.com/luomingui/p/3362813.html EF 5.0 帮助类 加入命名空间: using System; using System.D ...

  2. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  3. EF 6.0

    最近又开始研究EF框架了 哎 搞的东西太杂了 网上的参考了一篇博客 但是他是基于EF 4.0之前做的 所以自己基于他的博客来构造EF 6.0的使用基础 命名空间不同: 旧版本:using System ...

  4. ASP。netcore,Angular2 CRUD动画使用模板包,WEB API和EF 1.0.1

    下载Angular2ASPCORE.zip - 1 MB 介绍 在本文中,让我们看看如何创建一个ASP.NET Core CRUD web应用程序与Angular2动画使用模板包,web API和EF ...

  5. EF Core3.0+ 通过拦截器实现读写分离与SQL日志记录

    前言 本文主要是讲解EF Core3.0+ 通过拦截器实现读写分离与SQL日志记录 注意拦截器只有EF Core3.0+ 支持,2.1请考虑上下文工厂的形式实现. 说点题外话.. 一晃又大半年没更新技 ...

  6. Aspose.Cells for .NET 8.5.0 工具类

    基于 Aspose.Cells for .NET 8.5.0 工具类, Aspose.Cells for .NET 8.5.0 这个自己去CSDN下载里面有破解的,没有破解的导出excel的时候会(A ...

  7. 5.翻译:EF基础系列---EF中的上下文类

    原文地址:http://www.entityframeworktutorial.net/basics/context-class-in-entity-framework.aspx EF中的上下文类是一 ...

  8. ADO.NET Entity Framework CodeFirst 如何输出日志(EF 5.0)

    ADO.NET Entity Framework CodeFirst 如何输出日志(EF4.3) 用的EFProviderWrappers ,这个组件好久没有更新了,对于SQL执行日志的解决方案的需求 ...

  9. 一步步学习EF Core(3.EF Core2.0路线图)

    前言 这几天一直在研究EF Core的官方文档,暂时没有发现什么比较新的和EF6.x差距比较大的东西. 不过我倒是发现了EF Core的路线图更新了,下面我们就来看看 今天我们来看看最新的EF Cor ...

随机推荐

  1. 【Visual Lisp】驱动器、目录、文件和注册表

    ;;驱动器.目录.文件.和注册表;;★★★01.获取并创建驱动器盘符组成的表★★★(setq Drives (vlax-get-property (vlax-create-object "S ...

  2. Eclipse中.setting目录下文件介绍

    Eclipse项目中系统文件介绍 一. 写在前面 文章较长,可以直接到感兴趣的段落,或者直接关键字搜索: 请原谅作者掌握的编程语言少,这里只研究Java相关的项目: 每一个文件仅仅做一个常见内容的简单 ...

  3. 【Win10】解决 模拟器调试手机 错误-> 引导阶段... 无法找到指定路径......\2052\msdbgui.dll

    去弄几天的Web服务,又弄了几天的CefSharp,都是给其它组的同学做了一下支持,终于又可以回来玩下Win10啦. 今天想试一下UWP在手机上的效果,就找了台WP手机开始升级,结果下载速度遥遥无期, ...

  4. java开发常用jar包介绍(转载)

    jta.jar 标准JTA API必要 commons-collections.jar 集合类 必要 antlr.jar  ANother Tool for Language Recognition ...

  5. eclipse 远程调试

    http://blog.sina.com.cn/s/blog_86a6730b0101iean.html 注:远程服务器端可用以下方式替代: iptables -I from_external 3 - ...

  6. AT&T Assembly for Linux and Mac (sys_exit)

    Exit() in C : (sys_exit.c) int main(void) { ; } Exit() in AT&T for Linux: (sys_exit.s) .section ...

  7. Rails下cloud datastore的使用

      Rails下cloud datastore的使用 背景 部门有一个项目要用Ruby做 WebAPI,DB使用关系型数据库Cloud Sql和非关系型数据库Cloud Datastore . 还不了 ...

  8. Java的从浅至深绕坑而行的学习

    package day02; /** * 1:java初学习,避免面试时一些HR挖的坑. * @author biexiansheng * */ public class Test02 { publi ...

  9. JAVA开发工具eclipse中@author怎么改

    1:JAVA开发工具eclipse中@author怎么改,开发的时候为了注明版权信息. 用eclipse开发工具默认的是系统用户,那么怎么修改呢 示例如图所示 首先打开Eclipse--->然后 ...

  10. 分享45个设计师应该见到的新鲜的Web移动设备用户界面PSD套件

    对于一个网页设计师来说做一个好的PSD模板是非常有挑战性的一项任务,虽然PSD的模板简化了设计任务,但找出高质量的PSD文件,可以自由使用,是一 项艰巨的任务.你必须通过许多网页去找出一个极少数的PS ...