博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EF 5.0 帮助类 增删改查
阅读量:6965 次
发布时间:2019-06-27

本文共 4562 字,大约阅读时间需要 15 分钟。

原文地址:http://www.cnblogs.com/luomingui/p/3362813.html

EF 5.0 帮助类

加入命名空间:

using System;using System.Data;using System.Data.Entity;using System.Data.Entity.Infrastructure;using System.Linq;

接口:

public interface IEFRepository
where TEntity : class { bool AddEntity(TEntity entity); bool UpdateEntity(TEntity entity); bool UpdateEntity(IEnumerable
entities); bool DeleteEntity(int ID); bool DeleteEntity(TEntity entity); bool DeleteEntity(Expression
> predicate); bool DeleteEntity(IEnumerable
entities); IList
LoadEntities(Func
whereLambda); IList
LoadEntities(int pageIndex = 1, int pageSize = 30, Func
whereLambda = null); TEntity FindByID(int ID); }

具体类:

//EF5.0的写法     public class EFRepository
: IEFRepository
where TEntity : class { #region 单利模式 public static EFRepository
Instance = new EFRepository
(); public EFRepository( ) { Create(); } #endregion ///
/// 获取 当前使用的数据访问上下文对象 /// public DbContext Context { get { return EFDbContextHelper.Context; } } public bool AddEntity(TEntity entity) { EntityState state = Context.Entry(entity).State; if (state == EntityState.Detached) { Context.Entry(entity).State = EntityState.Added; } Context.SaveChanges(); return true; } public bool UpdateEntity(TEntity entity) { Context.Set
().Attach(entity); Context.Entry
(entity).State = EntityState.Modified; return Context.SaveChanges() > 0; } public bool UpdateEntity(IEnumerable
entities) { try { Context.Configuration.AutoDetectChangesEnabled = false; foreach (TEntity entity in entities) { UpdateEntity(entity); } return true; } finally { Context.Configuration.AutoDetectChangesEnabled = true; } } public bool DeleteEntity(int ID) { TEntity entity = FindByID(ID); return DeleteEntity(entity); } public bool DeleteEntity(TEntity entity) { Context.Set
().Attach(entity); Context.Entry
(entity).State = EntityState.Deleted; return Context.SaveChanges() > 0; } public bool DeleteEntity(System.Linq.Expressions.Expression
> predicate) { List
entities = Set().Where(predicate).ToList(); return Context.SaveChanges() > 0; } public bool DeleteEntity(IEnumerable
entities) { try { Context.Configuration.AutoDetectChangesEnabled = false; foreach (TEntity entity in entities) { DeleteEntity(entity); } return true; } finally { Context.Configuration.AutoDetectChangesEnabled = true; } } public IList
LoadEntities(Func
whereLambda) { if (whereLambda != null) return Context.Set
().Where
(whereLambda).AsQueryable().ToList(); else return Context.Set
().AsQueryable().ToList(); } public IList
LoadEntities(int pageIndex = 1, int pageSize = 30, Func
whereLambda = null) { int skinCount = (pageIndex - 1) * pageSize; if (whereLambda != null) return Set() .Where
(whereLambda) .Skip(skinCount) .Take(pageSize) .ToList(); else return Set() .Skip(skinCount) .Take(pageSize) .ToList(); } public DbSet
Set( ) { return Context.Set
(); } public TEntity FindByID(int ID) { return Set().Find(ID); } private TEntity Create( ) { return Context.Set
().Create(); } }

 

使用:

准备实体类

///      /// 实体类楼层管理 。(属性说明自动提取数据库字段的描述信息)     ///      [Serializable]    public class Floor     {         public int ID { get; set; }         [Category("楼层名称")]        public string f_Name { get; set; }         [Category("备注")]        public string f_Remark { get; set; }     }

使用EF帮助类调用

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

扩展:

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

你可能感兴趣的文章
MySQL索引分析
查看>>
20170829
查看>>
android项目获得所有运行程序
查看>>
Volley框架学习
查看>>
css中常用的标签
查看>>
android studio无线真机调试------Android
查看>>
jsp指令与动作
查看>>
C++中关键字的理解--Static
查看>>
简述Core Location定位功能
查看>>
html搜索,文中的关键字变色
查看>>
Python标准库_ sys,random,time
查看>>
[Android开发Tips]Bean的定义
查看>>
less
查看>>
PrestaShop 网站后台配置(三)
查看>>
【Win8启动后自动进入传统桌面设置】
查看>>
GP通过外部表装载数据时遇到ERROR:extra data after last expected column解决方法
查看>>
hdu2639,第K优决策
查看>>
hdu1166 敌兵布阵
查看>>
gcd(辗转相除法)
查看>>
多个Silverlight应用程序如何共享一个DomainService
查看>>