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. C语言误区

    int date[10] ; //定义一个字长10的数组,从date[0]....date[9] static   char   c ;   //静态变量(有静态外部变量和静态局部变量),静态外部变量 ...

  2. 使用MyBatis Generator自动创建代码

    SSM框架--使用MyBatis Generator自动创建代码 1. 目录说明 使用自动生成有很多方式,可以在eclipse中安装插件,但是以下将要介绍的这种方式我认为很轻松,最简单,不需要装插件, ...

  3. Linux ubuntu 10.10安装OpenCv

    在windows系统下已经成功做出了一个打开摄像头并检测人脸的小程序了. 开始转战linux,因为最终目标是将程序移植到嵌入式开发板上面. 但是,问题接踵而至~ 首先linux上面要安装OpenCv, ...

  4. java基础学习总结——this关键字

    一.this关键字

  5. (原创)即使最可怕的自然力量,也不失美丽——火山喷发(摄影,欣赏)

    文中图片摘自腾讯文化:www.cal.qq.com 1.Abstract     最可怕的力量也潜含着最美丽的风景奇观,虽然不能亲眼目睹,但透过大师的视角,一样也能体会到自然力量撼动的美丽. 2.Co ...

  6. [转] Linux中启动和停止jar包的运行

    脚本一: startTest.sh内容如下:#!/bin/shjava -jar Test.jar &       #注意:必须有&让其后台执行,否则没有pid生成echo $! &g ...

  7. SQLSERVER复制的要点

    SQLSERVER复制的要点 在论坛里经常有人问:SQLSERVER复制出问题了!!SQLSERVER复制不运行了!!SQLSERVER复制遇到阻塞了!! 然后最后来一句:“怎麽办?????????? ...

  8. Linux系列笔记 - 用户以及用户组命令

    一.前言 这一系列的随笔笔记,并不是详细的说明的命令的原理,只是简单的记录, 以备后期的查看以及复习 二.直接输入命令问题 有时候,我们在用 useradd groupadd等命令时,直接在终端输入的 ...

  9. jenkins插件 查看job下次运行时间

    文章来自:http://www.ciandcd.com 文中的代码来自可以从github下载: https://github.com/ciandcd jenkins插件next executions( ...

  10. Jetty 9嵌入式开发

    官方网址:http://www.eclipse.org/jetty/ 下载地址:http://download.eclipse.org/jetty/stable-9/dist/ 文档网址:http:/ ...