EF数据操作(二)
EF的处理基类EFRepository
1 /// <summary> 2 /// EF处理基类 3 /// </summary> 4 /// <typeparam name="TContext"></typeparam> 5 public class EFRepository<TContext> : IEFRepository 6 where TContext : DbContext, new() 7 { 8 //数据上下文 9 private TContext _dbContext; 10 /// <summary> 11 /// 数据上下文 12 /// </summary> 13 protected TContext DbContext 14 { 15 get 16 { 17 if (_dbContext == null) 18 { 19 _dbContext = new TContext(); 20 } 21 return _dbContext; 22 } 23 } 24 25 //MS SQL数据操作 26 private DbHelper _dbAccess; 27 /// <summary> 28 /// MS SQL数据操作 29 /// </summary> 30 private DbHelper DbAccess 31 { 32 get 33 { 34 if (_dbAccess == null) 35 { 36 _dbAccess = new DbHelper(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); 37 } 38 return _dbAccess; 39 } 40 } 41 42 #region IRepository 43 44 /// <summary> 45 /// 获取Table集合 46 /// </summary> 47 /// <typeparam name="TEntity"></typeparam> 48 /// <returns></returns> 49 public DbSet<TEntity> Tables<TEntity>() where TEntity : class 50 { 51 return DbContext.Set<TEntity>(); 52 } 53 54 /// <summary> 55 /// 查询 56 /// </summary> 57 /// <typeparam name="TEntity"></typeparam> 58 /// <returns></returns> 59 public IQueryable<TEntity> Get<TEntity>() where TEntity : class 60 { 61 return Tables<TEntity>(); 62 } 63 64 65 /// <summary> 66 /// 查询 67 /// </summary> 68 /// <typeparam name="TEntity"></typeparam> 69 /// <param name="expression"></param> 70 /// <returns></returns> 71 public IQueryable<TEntity> Get<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class 72 { 73 IQueryable<TEntity> query = Tables<TEntity>(); 74 if (expression != null) 75 query = Tables<TEntity>().Where(expression); 76 return query; 77 } 78 79 /// <summary> 80 /// 获取服务器时间 81 /// </summary> 82 /// <returns></returns> 83 public DateTime GetDate() 84 { 85 try 86 { 87 string sql = "select GetDate()"; 88 DateTime dt = DbContext.Database.SqlQuery<DateTime>(sql).Cast<DateTime>().FirstOrDefault(); 89 return dt; 90 } 91 catch 92 { 93 return DateTime.Now; 94 } 95 } 96 97 /// <summary> 98 /// 执行sql语句,返回datatable 99 /// </summary> 100 /// <param name="sql"></param> 101 /// <returns></returns> 102 public DataTable SqlQuery(string sql) 103 { 104 return DbAccess.GetDataTable(sql); 105 } 106 107 public IEnumerable<DbEntityValidationResult> GetValidationErrors() 108 { 109 return this.DbContext.GetValidationErrors(); 110 } 111 112 113 /// <summary> 114 /// 提交变更 115 /// </summary> 116 public int SubmitChanges() 117 { 118 return this.DbContext.SaveChanges(); 119 } 120 121 /// <summary> 122 /// 释放资源 123 /// </summary> 124 public void Dispose() 125 { 126 if (_dbContext != null) 127 { 128 this.DbContext.Dispose(); 129 _dbContext.Dispose(); 130 _dbContext = null; 131 } 132 } 133 134 #endregion 135 136 /// <summary> 137 /// 设置监听可用性 138 /// </summary> 139 /// <param name="isEnabled"></param> 140 public void SetDetectChanges(bool isEnabled) 141 { 142 this.DbContext.Configuration.AutoDetectChangesEnabled = isEnabled; 143 } 144 145 /// <summary> 146 /// 设置延迟加载可用性 147 /// </summary> 148 /// <param name="isEnabled"></param> 149 public void SetLazyLoading(bool isEnabled) 150 { 151 this.DbContext.Configuration.LazyLoadingEnabled = isEnabled; 152 } 153 }
EF处理基类对应的接口
1 public interface IEFRepository : IDisposable 2 { 3 /// <summary> 4 /// 获取Table集合 5 /// </summary> 6 /// <typeparam name="TEntity"></typeparam> 7 /// <returns></returns> 8 DbSet<TEntity> Tables<TEntity>() where TEntity : class; 9 10 /// <summary> 11 /// 查询 12 /// </summary> 13 /// <typeparam name="TEntity"></typeparam> 14 /// <returns></returns> 15 IQueryable<TEntity> Get<TEntity>() where TEntity : class; 16 17 /// <summary> 18 /// 查询 19 /// </summary> 20 /// <typeparam name="TEntity"></typeparam> 21 /// <param name="expression"></param> 22 /// <returns></returns> 23 IQueryable<TEntity> Get<TEntity>(Expression<Func<TEntity, bool>> expression) where TEntity : class; 24 25 /// <summary> 26 /// 执行sql语句,返回datatable 27 /// </summary> 28 /// <param name="sql"></param> 29 /// <returns></returns> 30 DataTable SqlQuery(string sql); 31 32 /// <summary> 33 /// 提交变更 34 /// </summary> 35 int SubmitChanges(); 36 37 IEnumerable<DbEntityValidationResult> GetValidationErrors(); 38 }