ef操作基类示例
来源:互联网 发布:linux切换用户权限命令 编辑:程序博客网 时间:2024/06/08 17:13
下面送上ef操作基类
public partial class BaseDal<T> where T:class { //DbContext context=new HM13OAContainer(); private DbContext context = ContextFactory.GetContext(); //增加 public int Add(T userInfo) { context.Set<T>().Add(userInfo); //return context.SaveChanges(); return 0; } //修改 public int Edit(T userInfo) { context.Entry(userInfo).State = EntityState.Modified; //return context.SaveChanges(); return 0; } //删除 public int Remove(int id) { T u1 = context.Set<T>().Find(id); //context.Set<T>().Remove(u1); //return context.SaveChanges(); context.Entry(u1).Property("IsDelete").IsModified = true; context.Entry(u1).Property("IsDelete").CurrentValue = true; return 0; } public int Remove(int[] ids) { int counter = ids.Length; for (int i = 0; i < counter; i++) { //T u1 = context.Set<T>().Find(ids[i]); //context.Set<T>().Remove(u1); Remove(ids[i]); } //return context.SaveChanges(); return 0; } public int Remove(T userInfo) { //context.Set<T>().Remove(userInfo); //return context.SaveChanges(); //实现软删除 context.Set<T>().Attach(userInfo); context.Entry(userInfo).Property("IsDelete").IsModified = true; context.Entry(userInfo).Property("IsDelete").CurrentValue = true; return 0; } //查询 public T GetById(int id) { return context.Set<T>().Find(id); } public IQueryable<T> GetList(Expression<Func<T, bool>> whereLambda) { return context.Set<T>().Where(whereLambda); //IEnumerable:select * from userinfo //IQueryable:select * from userinfo where ... } public IQueryable<T> GetPageList<TKey>(Expression<Func<T,bool>> whereLambda,Expression<Func<T,TKey>> orderLambda,int pageSize,int pageIndex,out int counter) { counter = context.Set<T>().Where(whereLambda).Count(); return context.Set<T>().Where(whereLambda) .OrderByDescending(orderLambda) .Skip((pageIndex - 1)*pageSize) .Take(pageSize); } public int GetAllCount() { using (DbContext Entities = ContextFactory.GetContext()) { return Entities.Set<T>().Count(); } } public IEnumerable<T> GetAllEntities() { using (DbContext Entities = ContextFactory.GetContext()) { return Entities.Set<T>().ToList(); } } public int GetCount(Func<T,bool> exp) { using (DbContext Entities = ContextFactory.GetContext()) { return Entities.Set<T>().Where(exp).Count(); } } public IEnumerable<T> GetEntities(Func<T, bool> exp) { using (DbContext Entities = ContextFactory.GetContext()) { return Entities.Set<T>().Where(exp).ToList(); } } }
0 0
- ef操作基类示例
- EF浅析及示例分析
- EF 跨数据库操作
- EF的操作
- EF操作-删除记录
- EF操作-事务
- 【EF】EF中的对表操作
- EF实体类的增删改查操作
- Entity Framework(EF) Code First 多对多关系的实体增,删,改,查操作全程详细示例
- EF操作增删改查
- 用EF框架操作数据库
- EF操作存储过程调用
- EF操作-修改部分字段
- EF是怎么操作数据库
- EF中的那些批量操作
- EF浅析及示例分析2
- EF-增删改查简单示例
- 【EF操作】——CRUD操作步骤
- React+Redux实现简单的待办事项列表ToDoList
- CountDownLatch详解
- Leetcode 474 - Ones and Zeroes(01背包)
- 蓝桥杯 九宫重排
- 【VScode】VScode 调用浏览器预览HTML (windows+ubuntu方法)
- ef操作基类示例
- Appium 新手贴:Windows 平台上的使用 Python 语言实现 appium 自动化程序 for Android (完整版)
- Markdown语法测试1
- Java 的两种重要机制
- Codeforces Round #394(Div. 2)B. Dasha and friends【思维+暴力】
- 如何使用EA画ER图?
- Tango 开发指南 — 区域学习 API (Java 描述)
- ubuntn16.04 安装msgpack
- Tautology POJ - 3295