原文地址: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<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就可以做到扩展了。

EF 5.0 帮助类 增删改查的更多相关文章

  1. Yii 1.1.17 四、属性标签、AR类增删改查、使用上传类与扩展第三方类库

    一.属性标签与规则设置 当进入网站页面,将会读数据库返回信息到视图上.那么,现在定义模型中的属性在视图标签上的显示, 也就是模型属性到前台标签的映射 // 定义模型属性到前台标签的映射 public ...

  2. EF(Entity Framework)通用DBHelper通用类,增删改查以及列表

    其中 通用类名:DBhelper 实体类:UserInfo 1 //新增 2 DBHelper<UserInfo> dbhelper = new DBHelper<UserInfo& ...

  3. 潭州课堂25班:Ph201805201 django框架 第六课 模型类增删改查,常用 的查询矣查询条件 (课堂笔记)

    在视图函数中写入增删改查的方法 增: 在 urls 中配置路径 : 查: 1: 在后台打印数据 在模型类中添加格式化输出 : QuerySet,反回的是个对象,可以按索引聚会,用 for 循环,, 找 ...

  4. MongoDB 3.0.6 安装 增删改查

    下载 安装包MSI http://yunpan.cn/cmhHdTPkXZRM2  访问密码 9b6c 上边提供的是 MongoDB 3.0.6 64Bit 的安装包 安装 如果不想直接安装在C盘.. ...

  5. WFS1.1.0协议(增删改查)+openlayers4.3.1前端构建+geoserver2.15.1安装部署+shpfile数据源配置

    WFS简介 1.WFS即,Web要素服务,全称WebFeatureService.GIS下,支持对地理要素的插入,更新,删除,检索和发现服务. 2.属于OGC标准下的通信协议.OGC标准下的GIS服务 ...

  6. django-rest-framework框架 第三篇 之CRUD视图扩展类(增删改查的优化)

    CRUD视图扩展类 1 CreateModelMixin 2 RetrieveModelMixin 3 UpdateModelMixin 4 DestroyModelMixin <1> 创 ...

  7. php单例模式封装数据库操作类增删改查

    <?php//三私一公 单例class Db{ //数据库连接对象 private static $instance; private static $table_name; private $ ...

  8. Yii2.0数据库操作增删改查详解

    1.简单查询: one(): 根据查询结果返回查询的第一条记录. all(): 根据查询结果返回所有记录. count(): 返回记录的数量. sum(): 返回指定列的总数. average():  ...

  9. EntityFramework经典数据访问层基类——增删改查

    namespace StudentSys.DAL { public class BaseService<T>:IDisposable where T:BaseEntity,new() { ...

随机推荐

  1. Android Studio中解决Gradle DSL method not found: &#39;android()&#39;

    近期导入as的项目出了这种问题 这个问题困扰了我非常长时间,好吧,搜了半天全都是runProguard的.最后在stackoverflow上搜到解决的方法了: http://stackoverflow ...

  2. Spring Remoting by HTTP Invoker Example--reference

    Spring provides its own implementation of remoting service known as HttpInvoker. It can be used for ...

  3. Java Numeric Formatting--reference

    I can think of numerous times when I have seen others write unnecessary Java code and I have written ...

  4. Extjs4学习

    1 Ext js初步 1.1 获取Extjs 下载extjs: 可以从http://extjs.org.cn/ 获得需要的extjs发布包及更多支持. 1.2 搭建学习环境: 假设您的机器已经安装my ...

  5. ImageView设置点击效果没有用?ImageView src的图片大小改变不了?

    ImageView设置点击效果没有用? 解决 1.ImageView xml里面必须clickable 和longClickable为true <ImageView android:layout ...

  6. Android实用代码块

    通过反射实现Menu显示图标 @Override public boolean onCreateOptionsMenu(Menu menu) { setIconEnable(menu, true); ...

  7. iOS8中添加的extensions总结(四)——Action扩展

    Action扩展 注:此教程来源于http://www.raywenderlich.com的<iOS8 by Tutorials> 1.准备 本次教程利用网站bitly.com进行 bit ...

  8. Php RSS

    RSS 聚合最近非常流行,因此至少对 RSS 及其工作方式有所了解是一名 PHP 开发人员的迫切需要.本文介绍了 RSS 基础知识.RSS 众多用途中的一些用途.如何使用 PHP 从数据库创建 RSS ...

  9. C#网络编程之WebClient

    1.什么是WebClient? 源自MSDN:提供用于将数据发送到由 URI 标识的资源及从这样的资源接收数据的常用方法. 2.OpenRead()  为从具有String指定的URI的资源下载的数据 ...

  10. Knockoutjs官网翻译系列(三) 使用Computed Observables

    书接上回,前面谈到了在视图模型中可以定义普通的observable属性以及observableArray属性实现与UI元素的双向绑定,这一节我们继续探讨第三种可实现绑定的属性类型:computed o ...