EF 5.0 帮助类

加入命名空间:

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

接口:

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

具体类:

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

使用:

准备实体类

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

使用EF帮助类调用

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

扩展:

其他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. Position和anchorPoint

    Main.storyboard ViewController.m // //  ViewController.m //  7A12.position和anchorPoint // //  Create ...

  2. utime函数

    utime函数:对一个文件的访问和修改时间 #include <utime.h> int utime( const char *pathname, const struct utimbuf ...

  3. linux知识点总结与随笔(关注linux爱好者公众号的一些笔记)

    sysdig工具,可以有strace ,tcpdump,lsof的功能. 前台任务与后台任务,知识点:test.sh &,Ctrl+z,bg,shopt grep |huponexit(sho ...

  4. oracle密码文件管理

    密码文件 密码文件作用: 密码文件用于dba用户的登录认证. dba用户:具备sysdba和sysoper权限的用户,即oracle的sys和system用户. 本地登录: 1)操作系统认证: [or ...

  5. SSH Secure Shell Client的windows客户端样式设置

    SSH Secure Shell Client下载:http://pan.baidu.com/s/1dF2lDdf 其他工具(putty-0.67)下载:http://pan.baidu.com/s/ ...

  6. Asp.Net Web API 2第二课——CRUD操作

    详情请查看http://aehyok.com/Blog/Detail/69.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  7. 第十八章:Android 打包部署

    Andriod应用程序如果要在手机或模拟器上安装,必须要有签名! 1.签名的意义 为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序, ...

  8. MVC3不能正确识别JSON中的Enum枚举值

    一.背景 在MVC3项目里,如果Action的参数中有Enum枚举作为对象属性的话,使用POST方法提交过来的JSON数据中的枚举值却无法正确被识别对应的枚举值. 二.Demo演示 为了说明问题,我使 ...

  9. [JS] HTML QQ分享界面js代码

    @ - @ :可以自己指定分享内容等....内含JS脚本 <html> <head> <title></title> <script type=& ...

  10. Windows Phone 8 解锁提示IpOverUsbSvc问题——IpOverUsbEnum返回No connected partners found解决方案

    我的1520之前总是无法解锁,提示:IpOverUsbSvc服务没有开启什么的. 根据网上网友的各种解决方案: 1. 把手机时间设置为当前时间,并且关闭“自动设置” 2. 确保手机接入了互联网 3.确 ...