using CZBK.HeiMaOA.Model; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading.Tasks; namespace CZBK.HeiMaOA.DAL { public class DBContextFactory { /// <summary> /// 保证在一次请求过程中只创建一次EF上下文实例. /// </summary> /// <returns></returns> public static DbContext CreateDbContext() { DbContext dbContext = (DbContext)CallContext.GetData("dbContext"); if (dbContext == null) { dbContext = new OAEntities(); CallContext.SetData("dbContext", dbContext); } return dbContext; } } }
using CZBK.HeiMaOA.Model; using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CZBK.HeiMaOA.DAL { public class BaseDal<T>where T:class,new() { // OAEntities Db = new OAEntities(); DbContext Db = DBContextFactory.CreateDbContext();//完成EF上下文创建. /// <summary> /// 基本查询方法 /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public IQueryable<T> LoadEntities(System.Linq.Expressions.Expression<Func<T, bool>> whereLambda) { return Db.Set<T>().Where<T>(whereLambda); } /// <summary> /// 分页方法 /// </summary> /// <typeparam name="s">排序的约束</typeparam> /// <param name="pageIndex">当前页码</param> /// <param name="pageSize">每页显示记录数</param> /// <param name="totalCount">总条数</param> /// <param name="whereLambda">过滤条件</param> /// <param name="orderbyLambda">排序条件</param> /// <param name="isAsc">排序方式</param> /// <returns></returns> public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, System.Linq.Expressions.Expression<Func<T, bool>> whereLambda, System.Linq.Expressions.Expression<Func<T, s>> orderbyLambda, bool isAsc) { var temp = Db.Set<T>().Where<T>(whereLambda); totalCount = temp.Count(); if (isAsc)//如果成立表示升序 { temp = temp.OrderBy<T, s>(orderbyLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize); } else { temp = temp.OrderByDescending<T, s>(orderbyLambda).Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize); } return temp; } /// <summary> /// 删除数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool DeleteEntity(T entity) { Db.Entry<T>(entity).State = System.Data.EntityState.Deleted; // return Db.SaveChanges() > 0; return true; } /// <summary> /// 更新 /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool UpdateEntity(T entity) { Db.Entry<T>(entity).State = System.Data.EntityState.Modified; // return Db.SaveChanges() > 0; return true; } /// <summary> /// 添加数据 /// </summary> /// <param name="entity"></param> /// <returns></returns> public T AddEntity(T entity) { Db.Set <T>().Add(entity); //Db.SaveChanges(); return entity; } } }
时间: 2024-11-14 11:45:58