参考了网上EF数据处理的代码写成
在UPDATE的方法不太好,性能可能有问题。大家如果有什么好的方法可以给我留言。
暂时没有想到
接口类
using System; using System.Collections.Generic; namespace Model.EF { public interface IRepository<TEntity> where TEntity : IEntity { TEntity FindById(int ID); #region 查询普通实现方案(基于Lambda表达式的Where查询) /// <summary> /// 获取所有Entity /// </summary> /// <param name="exp">Lambda条件的where</param> /// <returns></returns> IEnumerable<TEntity> GetEntities(Func<TEntity, bool> exp); /// <summary> /// 计算总个数(分页) /// </summary> /// <param name="exp">Lambda条件的where</param> /// <returns></returns> int GetEntitiesCount(Func<TEntity, bool> exp); /// <summary> /// 分页查询(Linq分页方式) /// </summary> /// <param name="pageNumber">当前页</param> /// <param name="pageSize">页码</param> /// <param name="orderName">lambda排序名称</param> /// <param name="sortOrder">排序(升序or降序)</param> /// <param name="exp">lambda查询条件where</param> /// <returns></returns> IEnumerable<TEntity> GetEntitiesForPaging(int pageNumber, int pageSize, Func<TEntity, string> orderName, string sortOrder, Func<TEntity, bool> exp); /// <summary> /// 根据条件查找 /// </summary> /// <param name="exp">lambda查询条件where</param> /// <returns></returns> TEntity GetEntity(Func<TEntity, bool> exp); #endregion #region 查询Sql语句外接接口的查询实现 /// <summary> /// 获取所有Entity(立即执行请使用ToList() /// </summary> /// <param name="commandText">Sql语句</param> /// <param name="objParams">可变参数</param> /// <returns></returns> IEnumerable<TEntity> GetEntities(string commandText); /// <summary> /// 计算总个数(分页) /// </summary> /// <param name="tableName">表名</param> /// <param name="commandText">>Sql where语句</param> /// <returns></returns> int GetEntitiesCount(string tableName, string commandText); /// <summary> /// 计算总个数(分页) /// </summary> /// <param name="commandText">Sql语句</param> /// <param name="objParams">可变参数</param> /// <returns></returns> int GetEntitiesCount(string commandText); /// <summary> /// 分页查询(Linq分页方式) /// </summary> /// <param name="tableName">表名</param> /// <param name="pageNumber">当前页</param> /// <param name="pageSize">页码</param> /// <param name="orderName">lambda排序名称</param> /// <param name="sortOrder">排序(升序or降序)</param> /// <param name="commandText">Sql语句</param> /// <param name="count">总个数</param> /// <returns></returns> IEnumerable<TEntity> GetEntitiesForPaging(string tableName, int pageNumber, int pageSize, string orderName, string sortOrder, string commandText, out int count); /// <summary> /// 根据条件查找 /// </summary> /// <param name="commandText">Sql语句</param> /// <param name="objParams">可变参数</param> /// <returns></returns> TEntity GetEntity(string commandText); #endregion List<TEntity> FindBy(string where); List<TEntity> FindPageList(string where, int pageIndex, int pageSize, out int totalRecord); IEnumerable<TEntity> FindAll(); //IEnumerable<TEntity> FindAll(string query); IEnumerable<TEntity> FindBySpecification(Func<TEntity, bool> spec); int Add(TEntity entity); void Delete(TEntity entity); void Update(TEntity entity); void Update(TEntity entity, params string[] fileds); } }
实现类:
using System; using System.Collections.Generic; using System.Linq; using System.Data; using System.Data.Entity; using System.Data.Objects; using System.Data.Entity.Infrastructure; using Common; namespace Model.EF { public class EntityRepository<TEntity> : IRepository<TEntity> where TEntity : class, IEntity { private readonly NerdDinners m_dbContext; private ObjectContext context; public EntityRepository() { m_dbContext =new NerdDinners(); } protected virtual void OnLoaded(TEntity entity) { } protected DbSet<TEntity> DbSet { get { return m_dbContext.Set<TEntity>(); } } public virtual IEnumerable<TEntity> GetEntities(Func<TEntity, bool> exp) { return DbSet.Where<TEntity>(exp).ToList(); } public virtual int GetEntitiesCount(Func<TEntity, bool> exp) { return DbSet.Where<TEntity>(exp).ToList().Count; } public virtual TEntity GetEntity(Func<TEntity, bool> exp) { return DbSet.SingleOrDefault<TEntity>(exp); } public virtual IEnumerable<TEntity> GetEntitiesForPaging(int pageNumber, int pageSize, Func<TEntity, string> orderName, string sortOrder, Func<TEntity, bool> exp) { if (sortOrder=="asc") { return DbSet.Where<TEntity>(exp).OrderBy(orderName).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList(); }else{ return DbSet.Where<TEntity>(exp).OrderByDescending(orderName).Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList(); } } #region 查询Entity To Sql语句外接接口的查询实现 /// <summary> /// 获取所有Entity(立即执行请使用ToList() /// </summary> /// <param name="commandText">Sql语句</param> /// <param name="objParams">可变参数</param> /// <returns></returns> public virtual IEnumerable<TEntity> GetEntities(string commandText) { if (!commandText.IsNullOrEmpty()) commandText = " where " + commandText; var context = ((IObjectContextAdapter)DbSet).ObjectContext; return context.ExecuteStoreQuery<TEntity>("select * from " + typeof(TEntity).Name + commandText).ToList(); } /// <summary> /// 计算总个数(分页) /// </summary> /// <param name="tableName">表名</param> /// <param name="commandText">>Sql where语句</param> /// <returns></returns> public virtual int GetEntitiesCount(string tableName, string commandText) { if (!commandText.IsNullOrEmpty()) commandText = " where " + commandText; var context = ((IObjectContextAdapter)DbSet).ObjectContext; return context.ExecuteStoreQuery<TEntity>("select * from " + tableName + commandText).Count(); } /// <summary> /// 计算总个数(分页) /// </summary> /// <param name="CommandText">Sql语句</param> /// <returns></returns> public virtual int GetEntitiesCount(string CommandText) { if (!CommandText.IsNullOrEmpty()) CommandText = " where " + CommandText; var context = ((IObjectContextAdapter)DbSet).ObjectContext; return context.ExecuteStoreQuery<TEntity>("select * from " + typeof(TEntity).Name + CommandText).Count(); } /// <summary> /// 分页查询(Linq分页方式) /// </summary> /// <param name="tableName">表名</param> /// <param name="pageNumber">当前页</param> /// <param name="pageSize">页码</param> /// <param name="orderName">lambda排序名称</param> /// <param name="sortOrder">排序(升序or降序)</param> /// <param name="CommandText">Sql语句</param> /// <param name="Count">总个数</param> /// <returns></returns> public virtual IEnumerable<TEntity> GetEntitiesForPaging(string tableName, int pageNumber, int pageSize, string orderName, string sortOrder, string CommandText, out int Count) { PageHelper pager = new PageHelper(tableName, orderName, pageSize, pageNumber, sortOrder, CommandText); Count = GetEntitiesCount(tableName, CommandText); var context = ((IObjectContextAdapter)DbSet).ObjectContext; return context.ExecuteStoreQuery<TEntity>(pager.GetSelectTopByMaxOrMinPagination()).ToList(); } /// <summary> /// 根据条件查找 /// </summary> /// <param name="CommandText">Sql语句</param> /// <param name="objParams">可变参数</param> /// <returns></returns> public virtual TEntity GetEntity(string CommandText) { var context = ((IObjectContextAdapter)DbSet).ObjectContext; return context.ExecuteStoreQuery<TEntity>("select * from " + typeof(TEntity).Name + " where " + CommandText).SingleOrDefault(); } #endregion public IEnumerable<TEntity> FindAll() { return DbSet; } public List<TEntity> FindBy(string where) { var list = DbSet.SqlQuery(where); return list.ToList(); } public List<TEntity> FindPageList(string where, int pageIndex, int pageSize, out int totalRecord) { totalRecord = 0; var list = DbSet.SqlQuery(where).Skip((pageIndex - 1) * pageSize).Take(pageSize); totalRecord = DbSet.SqlQuery(where).Count(); return list.ToList(); } public TEntity FindById(int id) { return DbSet.SingleOrDefault(entity => entity.ID== id); } public IEnumerable<TEntity> FindBySpecification(Func<TEntity, bool> spec) { throw new NotImplementedException(); } public int Add(TEntity entity) { DbSet.Add(entity); return m_dbContext.SaveChanges(); } public void Delete(TEntity entity) { var entry = m_dbContext.Entry(entity); if (entry.State == EntityState.Detached) { //DbSet.Attach(entity); entry.State = EntityState.Deleted; //var entityToUpdate = FindById(entity.Id); //EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<TEntity, TEntity>().Map(entity, entityToUpdate); } m_dbContext.SaveChanges(); } public void Update(TEntity entity) { var entry = m_dbContext.Entry(entity); if (entry.State == EntityState.Detached) { //DbSet.Attach(entity); //entry.State = EntityState.Unchanged; var entityToUpdate = DbSet.Find(entity.ID); EmitMapper.ObjectMapperManager.DefaultInstance.GetMapper<TEntity, TEntity>().Map(entity, entityToUpdate); } m_dbContext.SaveChanges(); } public void Update(TEntity entity, params string[] fileds) { var entry = m_dbContext.Entry(entity); Type Ttype = typeof(TEntity); var entityToUpdate = FindById(entity.ID); context.AttachTo(Ttype.Name, entityToUpdate); var stateEntry = context.ObjectStateManager.GetObjectStateEntry(entity); for (int i = 0; i < fileds.Length - 1; i++) { stateEntry.SetModifiedProperty(fileds[i]); } m_dbContext.SaveChanges(); } } }
写的不好,有什么问题给我留言。