使用SqlSugar封装的数据层基类

首先简单封装了个DbContext

  1  public class DbContext
  2     {
  3         #region 属性字段
  4         private static string _connectionString;
  5 
  6         /// 
  7         /// 连接字符串 by beck.huang 2018-05-08 09:56:05
  8         /// 
  9         public static string ConnectionString
 10         {
 11             get { return _connectionString; }
 12             set { _connectionString = value; }
 13         }
 14 
 15         private static DbType _dbType;
 16 
 17         /// 
 18         /// 数据库类型 by beck.huang 2018-05-08 09:58:03
 19         /// 
 20         public static DbType DbType
 21         {
 22             get { return _dbType; }
 23             set { _dbType = value; }
 24         }
 25 
 26 
 27         private SqlSugarClient _db;
 28 
 29         /// 
 30         /// 数据连接对象 by beck.huang 2018-05-08 10:00:14
 31         /// 
 32         public SqlSugarClient Db
 33         {
 34             get { return _db; }
 35             private set { _db = value; }
 36         }
 37 
 38         /// 
 39         /// 数据库上下文实例(自动关闭连接) by beck.huang 2018-05-08 09:47:30
 40         /// 
 41         public static DbContext Context
 42         {
 43             get
 44             {
 45                 return new DbContext();
 46             }
 47 
 48         }
 49         #endregion
 50 
 51         #region 构造函数
 52 
 53         /// 
 54         /// 功能描述:构造函数
 55         /// 作  者:beck.huang
 56         /// 创建日期:2018-05-08 09:47:24
 57         /// 任务编号:中餐
 58         /// 
 59         private DbContext()
 60         {
 61             if (string.IsNullOrEmpty(_connectionString))
 62                 throw new ArgumentNullException("数据库连接字符串为空");
 63             _db = new SqlSugarClient(new ConnectionConfig()
 64             {
 65                 ConnectionString = _connectionString,
 66                 DbType = _dbType,
 67                 IsAutoCloseConnection = true,
 68                 IsShardSameThread = true,
 69                 ConfigureExternalServices = new ConfigureExternalServices()
 70                 {
 71                     DataInfoCacheService = new HttpRuntimeCache()
 72                 },
 73                 MoreSettings = new ConnMoreSettings()
 74                 {
 75                     //IsWithNoLockQuery = true,
 76                     IsAutoRemoveDataCache = true
 77                 }
 78             });
 79         }
 80 
 81         /// 
 82         /// 功能描述:构造函数
 83         /// 作  者:beck.huang
 84         /// 创建日期:2018-05-28 17:23:19
 85         /// 任务编号:中餐
 86         /// 
 87         /// 是否自动关闭连接
 88         private DbContext(bool blnIsAutoCloseConnection)
 89         {
 90             if (string.IsNullOrEmpty(_connectionString))
 91                 throw new ArgumentNullException("数据库连接字符串为空");
 92             _db = new SqlSugarClient(new ConnectionConfig()
 93             {
 94                 ConnectionString = _connectionString,
 95                 DbType = _dbType,
 96                 IsAutoCloseConnection = blnIsAutoCloseConnection,
 97                 IsShardSameThread = true,
 98                 ConfigureExternalServices = new ConfigureExternalServices()
 99                 {
100                     DataInfoCacheService = new HttpRuntimeCache()
101                 },
102                 MoreSettings = new ConnMoreSettings()
103                 {
104                     //IsWithNoLockQuery = true,
105                     IsAutoRemoveDataCache = true
106                 }
107             });
108         }
109         #endregion
110 
111         #region 实例方法
112         /// 
113         /// 功能描述:获取数据库处理对象
114         /// 作  者:beck.huang
115         /// 创建日期:2018-05-08 09:46:06
116         /// 任务编号:中餐
117         /// 
118         /// 返回值
119         public SimpleClient GetEntityDB() where T : class,new()
120         {
121             return new SimpleClient(_db);
122         }
123         /// 
124         /// 功能描述:获取数据库处理对象
125         /// 作  者:beck.huang
126         /// 创建日期:2018-05-09 09:17:43
127         /// 任务编号:中餐
128         /// 
129         /// db
130         /// 返回值
131         public SimpleClient GetEntityDB(SqlSugarClient db) where T : class,new()
132         {
133             return new SimpleClient(db);
134         }
135 
136         #region 根据数据库表生产实体类
137         /// 
138         /// 功能描述:根据数据库表生产实体类
139         /// 作  者:beck.huang
140         /// 创建日期:2018-05-08 10:14:37
141         /// 任务编号:中餐
142         ///        
143         /// 实体类存放路径
144         public void CreateClassFileByDBTalbe(string strPath)
145         {
146             CreateClassFileByDBTalbe(strPath, "Km.PosZC");
147         }
148         /// 
149         /// 功能描述:根据数据库表生产实体类
150         /// 作  者:beck.huang
151         /// 创建日期:2018-05-08 10:10:59
152         /// 任务编号:中餐
153         /// 
154         /// 实体类存放路径
155         /// 命名空间
156         public void CreateClassFileByDBTalbe(string strPath, string strNameSpace)
157         {
158             CreateClassFileByDBTalbe(strPath, strNameSpace, null);
159         }
160 
161         /// 
162         /// 功能描述:根据数据库表生产实体类
163         /// 作  者:beck.huang
164         /// 创建日期:2018-05-08 10:18:18
165         /// 任务编号:中餐
166         /// 
167         /// 实体类存放路径
168         /// 命名空间
169         /// 生产指定的表
170         public void CreateClassFileByDBTalbe(
171             string strPath,
172             string strNameSpace,
173             string[] lstTableNames)
174         {
175             CreateClassFileByDBTalbe(strPath, strNameSpace, lstTableNames, string.Empty);
176         }
177 
178         /// 
179         /// 功能描述:根据数据库表生产实体类
180         /// 作  者:beck.huang
181         /// 创建日期:2018-05-09 15:38:22
182         /// 任务编号:中餐
183         /// 
184         /// 实体类存放路径
185         /// 命名空间
186         /// 生产指定的表
187         /// 实现接口
188         public void CreateClassFileByDBTalbe(
189           string strPath,
190           string strNameSpace,
191           string[] lstTableNames,
192           string strInterface,
193           bool blnSerializable = false)
194         {
195             if (lstTableNames != null && lstTableNames.Length > 0)
196             {
197                 _db.DbFirst.Where(lstTableNames).IsCreateDefaultValue().IsCreateAttribute()
198                     .SettingClassTemplate(p => p = @"
199 {using}
200 
201 namespace {Namespace}
202 {
203     {ClassDescription}{SugarTable}" + (blnSerializable ? "[Serializable]" : "") + @"
204     public partial class {ClassName}" + (string.IsNullOrEmpty(strInterface) ? "" : (" : " + strInterface)) + @"
205     {
206         public {ClassName}()
207         {
208 {Constructor}
209         }
210 {PropertyName}
211     }
212 }
213 ")
214                     .SettingPropertyTemplate(p => p = @"
215             {SugarColumn}
216             public {PropertyType} {PropertyName}
217             {
218                 get
219                 {
220                     return _{PropertyName};
221                 }
222                 set
223                 {
224                     if(_{PropertyName}!=value)
225                     {
226                         base.SetValueCall(" + "\"{PropertyName}\",_{PropertyName}" + @");
227                     }
228                     _{PropertyName}=value;
229                 }
230             }")
231                     .SettingPropertyDescriptionTemplate(p => p = "          private {PropertyType} _{PropertyName};\r\n" + p)
232                     .SettingConstructorTemplate(p => p = "              this._{PropertyName} ={DefaultValue};")
233                     .CreateClassFile(strPath, strNameSpace);
234             }
235             else
236             {
237                 _db.DbFirst.IsCreateAttribute().IsCreateDefaultValue()
238                     .SettingClassTemplate(p => p = @"
239 {using}
240 
241 namespace {Namespace}
242 {
243     {ClassDescription}{SugarTable}" + (blnSerializable ? "[Serializable]" : "") + @"
244     public partial class {ClassName}" + (string.IsNullOrEmpty(strInterface) ? "" : (" : " + strInterface)) + @"
245     {
246         public {ClassName}()
247         {
248 {Constructor}
249         }
250 {PropertyName}
251     }
252 }
253 ")
254                     .SettingPropertyTemplate(p => p = @"
255             {SugarColumn}
256             public {PropertyType} {PropertyName}
257             {
258                 get
259                 {
260                     return _{PropertyName};
261                 }
262                 set
263                 {
264                     if(_{PropertyName}!=value)
265                     {
266                         base.SetValueCall(" + "\"{PropertyName}\",_{PropertyName}" + @");
267                     }
268                     _{PropertyName}=value;
269                 }
270             }")
271                     .SettingPropertyDescriptionTemplate(p => p = "          private {PropertyType} _{PropertyName};\r\n" + p)
272                     .SettingConstructorTemplate(p => p = "              this._{PropertyName} ={DefaultValue};")
273                     .CreateClassFile(strPath, strNameSpace);
274             }
275         }
276         #endregion
277 
278         #region 根据实体类生成数据库表
279         /// 
280         /// 功能描述:根据实体类生成数据库表
281         /// 作  者:beck.huang
282         /// 创建日期:2018-05-08 10:31:02
283         /// 任务编号:中餐
284         /// 
285         /// 是否备份表
286         /// 指定的实体
287         public void CreateTableByEntity(bool blnBackupTable, params T[] lstEntitys) where T : class,new()
288         {
289             Type[] lstTypes = null;
290             if (lstEntitys != null)
291             {
292                 lstTypes = new Type[lstEntitys.Length];
293                 for (int i = 0; i < lstEntitys.Length; i++)
294                 {
295                     T t = lstEntitys[i];
296                     lstTypes[i] = typeof(T);
297                 }
298             }
299             CreateTableByEntity(blnBackupTable, lstTypes);
300         }
301 
302         /// 
303         /// 功能描述:根据实体类生成数据库表
304         /// 作  者:beck.huang
305         /// 创建日期:2018-05-08 10:31:14
306         /// 任务编号:中餐
307         /// 
308         /// 是否备份表
309         /// 指定的实体
310         public void CreateTableByEntity(bool blnBackupTable, params Type[] lstEntitys)
311         {
312             if (blnBackupTable)
313             {
314                 _db.CodeFirst.BackupTable().InitTables(lstEntitys); //change entity backupTable            
315             }
316             else
317             {
318                 _db.CodeFirst.InitTables(lstEntitys);
319             }
320         }
321         #endregion
322 
323         #endregion
324 
325         #region 静态方法
326 
327         /// 
328         /// 功能描述:获得一个DbContext
329         /// 作  者:beck.huang
330         /// 创建日期:2018-05-28 17:24:12
331         /// 任务编号:中餐
332         /// 
333         /// 是否自动关闭连接(如果为false,则使用接受时需要手动关闭Db)
334         /// 返回值
335         public static DbContext GetDbContext(bool blnIsAutoCloseConnection)
336         {
337             return new DbContext(blnIsAutoCloseConnection);
338         }
339 
340         /// 
341         /// 功能描述:设置初始化参数
342         /// 作  者:beck.huang
343         /// 创建日期:2018-05-08 10:02:32
344         /// 任务编号:中餐
345         /// 
346         /// 连接字符串
347         /// 数据库类型
348         public static void Init(string strConnectionString, DbType enmDbType = SqlSugar.DbType.MySql)
349         {
350             _connectionString = strConnectionString;
351             _dbType = enmDbType;
352         }
353 
354         /// 
355         /// 功能描述:创建一个链接配置
356         /// 作  者:beck.huang
357         /// 创建日期:2018-05-09 09:03:33
358         /// 任务编号:中餐
359         /// 
360         /// 是否自动关闭连接
361         /// 是否夸类事务
362         /// ConnectionConfig
363         public static ConnectionConfig GetConnectionConfig(bool blnIsAutoCloseConnection = true, bool blnIsShardSameThread = false)
364         {
365             ConnectionConfig config = new ConnectionConfig()
366             {
367                 ConnectionString = _connectionString,
368                 DbType = _dbType,
369                 IsAutoCloseConnection = blnIsAutoCloseConnection,
370                 ConfigureExternalServices = new ConfigureExternalServices()
371                 {
372                     DataInfoCacheService = new HttpRuntimeCache()
373                 },
374                 IsShardSameThread = blnIsShardSameThread
375             };
376             return config;
377         }
378 
379         /// 
380         /// 功能描述:获取一个自定义的DB
381         /// 作  者:beck.huang
382         /// 创建日期:2018-05-08 09:49:36
383         /// 任务编号:中餐
384         /// 
385         /// config
386         /// 返回值
387         public static SqlSugarClient GetCustomDB(ConnectionConfig config)
388         {
389             return new SqlSugarClient(config);
390         }
391         /// 
392         /// 功能描述:获取一个自定义的数据库处理对象
393         /// 作  者:beck.huang
394         /// 创建日期:2018-05-09 08:56:20
395         /// 任务编号:中餐
396         /// 
397         /// sugarClient
398         /// 返回值
399         public static SimpleClient GetCustomEntityDB(SqlSugarClient sugarClient) where T : class,new()
400         {
401             return new SimpleClient(sugarClient);
402         }
403         /// 
404         /// 功能描述:获取一个自定义的数据库处理对象
405         /// 作  者:beck.huang
406         /// 创建日期:2018-05-08 09:50:32
407         /// 任务编号:中餐
408         /// 
409         /// config
410         /// 返回值
411         public static SimpleClient GetCustomEntityDB(ConnectionConfig config) where T : class,new()
412         {
413             SqlSugarClient sugarClient = GetCustomDB(config);
414             return GetCustomEntityDB(sugarClient);
415         }
416         #endregion
417     }
View Code

然后是一个BaseDal,基本上覆盖了大部分的增删改查功能

 public class BaseDal where T : class ,new()
    {
        #region 属性字段
        private DbContext _context;

        public DbContext Context
        {
            get { return _context; }
            set { _context = value; }
        }

        private SqlSugarClient _db;

        /// 
        /// 数据处理对象 by beck.huang 2018-05-09 09:31:24
        /// 
        internal SqlSugarClient Db
        {
            get { return _db; }
            private set { _db = value; }
        }

        private SimpleClient _entityDB;

        /// 
        /// 实体数据处理对象 by beck.huang 2018-05-09 09:31:15
        /// 
        internal SimpleClient EntityDB
        {
            get { return _entityDB; }
            private set { _entityDB = value; }
        }

        public Action OnDalError { get; set; }
        public Action<string, SugarParameter[]> OnDalLogExecuting { get; set; }
        public Action<string, SugarParameter[]> OnDalLogExecuted { get; set; }
        public Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> OnDalExecutingChangeSql { get; set; }
        #endregion

        #region 构造函数
        /// 
        /// 功能描述:构造函数
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:30:54
        /// 任务编号:中餐
        /// 
        /// 是否自动关闭连接
        public BaseDal(bool blnIsAutoCloseConnection = true)
        {
            DbContext context = DbContext.GetDbContext(blnIsAutoCloseConnection);
            _context = context;
            _db = context.Db;
            //_db.Aop.OnLogExecuted = new Action(OnLogExecuted);
            //_db.Aop.OnLogExecuting = OnDalLogExecuting;
            //_db.Aop.OnError = OnDalError;
            //_db.Aop.OnExecutingChangeSql = new Func>(OnExecutingChangeSql);
            _entityDB = context.GetEntityDB(_db);
        }

        /// 
        /// 功能描述:使用指定的DbContext初始化一个对象
        /// 作  者:beck.huang
        /// 创建日期:2018-05-28 17:31:57
        /// 任务编号:中餐
        /// 
        /// DbContext
        public BaseDal(DbContext context)
        {
            _context = context;
            _db = context.Db;
            _entityDB = context.GetEntityDB(_db);
        }
        #endregion

        #region/// 
        /// 功能描述:插入数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 17:57:26
        /// 任务编号:中餐
        /// 
        /// strSql
        /// parameters
        /// 是否成功
        public bool Insert(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.ExecuteCommand(strSql, parameters) > 0;
        }
        /// 
        /// 功能描述:插入数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:43:06
        /// 任务编号:中餐
        /// 
        /// 实体列表
        /// 是否成功
        public bool Insert(params T[] entitys)
        {
            if (entitys != null && entitys.Length > 0)
            {
                return _entityDB.InsertRange(entitys);
            }
            return true;
        }

        /// 
        /// 功能描述:插入数据,返回自增列
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:44:52
        /// 任务编号:中餐
        /// 
        /// 实体
        /// 自增ID
        public int InsertReturnIdentity(T entity)
        {
            return _entityDB.InsertReturnIdentity(entity);
        }
        #endregion

        #region/// 
        /// 功能描述:删除指定实体
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:47:38
        /// 任务编号:中餐
        /// 
        /// 实体(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 是否成功
        public bool Delete(T entity)
        {
            return _entityDB.Delete(entity);
        }

        /// 
        /// 功能描述:删除数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:06:10
        /// 任务编号:中餐
        /// 
        /// 实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 受影响行数
        public int Delete(T[] entitys)
        {
            if (entitys != null)
                return _db.Deleteable(entitys.ToList()).ExecuteCommand();
            else
                return 0;
        }
        /// 
        /// 功能描述:删除数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:52:35
        /// 任务编号:中餐
        /// 
        /// 条件表达式
        /// 是否成功
        public bool Delete(Expressionbool>> whereExpression)
        {
            return _entityDB.Delete(whereExpression);
        }
        /// 
        /// 功能描述:删除数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:02:37
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 是否成功
        public bool Delete(string strWhere)
        {
            return _db.Deleteable().Where(strWhere).ExecuteCommand() > 0;
        }
        /// 
        /// 功能描述:删除数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:02:37
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 是否成功
        public bool Delete(string strWhere, List lstParameters)
        {
            return _db.Deleteable().Where(strWhere, lstParameters).ExecuteCommand() > 0;
        }

        /// 
        /// 功能描述:根据ID删除
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:08:18
        /// 任务编号:中餐
        /// 
        /// 主键列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 受影响行数
        public int DeleteByID(params object[] ids)
        {
            return _db.Deleteable().In(ids).ExecuteCommand();
        }

        #endregion

        #region/// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 17:57:26
        /// 任务编号:中餐
        /// 
        /// strSql
        /// parameters
        /// 是否成功
        public bool Update(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.ExecuteCommand(strSql, parameters) > 0;
        }


        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:28:27
        /// 任务编号:中餐
        /// 
        /// 实体(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 是否成功
        public bool Update(T entity)
        {
            return _entityDB.Update(entity);
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:28:27
        /// 任务编号:中餐
        /// 
        /// 实体
        /// 条件
        /// 是否成功
        public bool Update(T entity, string strWhere)
        {
            return _db.Updateable(entity).Where(strWhere).ExecuteCommand() > 0;
        }
        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:28:27
        /// 任务编号:中餐
        /// 
        /// 实体
        /// 条件
        /// 参数
        /// 是否成功
        public bool Update(
            T entity,
            string strWhere,
            List lstParameters)
        {
            return _db.Updateable(entity).Where(strWhere, lstParameters).ExecuteCommand() > 0;
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:31:15
        /// 任务编号:中餐
        /// 
        /// 实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 受影响行数
        public int Update(T[] entitys)
        {
            return Update(entitys, null, null);
        }
        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 15:31:09
        /// 任务编号:中餐
        /// 
        /// 实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 受影响行数
        public int Update(
            T[] entitys,
            List<string> lstColumns,
            List<string> lstIgnoreColumns)
        {
            return Update(entitys, lstColumns, lstIgnoreColumns, "");
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 15:31:47
        /// 任务编号:中餐
        /// 
        /// 实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 条件
        /// 受影响行数
        public int Update(
            T[] entitys,
            List<string> lstColumns,
            List<string> lstIgnoreColumns,
            string strWhere)
        {
            return Update(entitys, lstColumns, lstIgnoreColumns, strWhere, null);
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 15:33:06
        /// 任务编号:中餐
        /// 
        /// 实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 条件
        /// 条件参数
        /// 受影响行数
        public int Update(
            T[] entitys,
            List<string> lstColumns,
            List<string> lstIgnoreColumns,
            string strWhere,
            List lstParameters)
        {
            IUpdateable up = _db.Updateable(entitys);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere, lstParameters);
            }
            return up.ExecuteCommand();
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 15:33:36
        /// 任务编号:中餐
        /// 
        /// 实体列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 条件
        /// 受影响行数
        public int Update(
            T[] entitys,
            List<string> lstColumns,
            List<string> lstIgnoreColumns,
            Expressionbool>> whereExpression)
        {
            IUpdateable up = _db.Updateable(entitys);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            return up.ExecuteCommand();
        }
        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:40:18
        /// 任务编号:中餐
        /// 
        /// 修改的列
        /// 条件表达式
        /// 是否成功
        public bool Update(Expression> columns, Expressionbool>> whereExpression)
        {
            return _entityDB.Update(columns, whereExpression);
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:43:35
        /// 任务编号:中餐
        /// 
        /// 实体(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 是否成功
        public bool Update(
            T entity,
            List<string> lstColumns,
            List<string> lstIgnoreColumns)
        {
            return Update(entity, lstColumns, lstIgnoreColumns, string.Empty);
        }



        /// 
        /// 功能描述:修改指定的列和值
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 19:33:46
        /// 任务编号:中餐
        ///        
        /// 条件
        /// 列和值列表(如:it => it.Name == (it.Name + 1))
        /// 是否成功
        public bool Update(string strWhere, params Expressionbool>>[] lstSetValueExpression)
        {
            return Update(strWhere, null, lstSetValueExpression);
        }

        /// 
        /// 功能描述:修改指定的列和值
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 19:34:01
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 列和值列表(如:it => it.Name == (it.Name + 1))        
        /// 是否成功
        public bool Update(
            string strWhere,
            List lstParameters,
            params Expressionbool>>[] lstSetValueExpression
            )
        {
            IUpdateable up = _db.Updateable();
            if (lstSetValueExpression != null)
            {
                foreach (var item in lstSetValueExpression)
                {
                    up = up.ReSetValue(item);
                }
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere, lstParameters);
            }

            return up.ExecuteCommand() > 0;
        }



        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:43:35
        /// 任务编号:中餐
        /// 
        /// 实体
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 条件
        /// 是否成功
        public bool Update(
            T entity,
            List<string> lstColumns,
            List<string> lstIgnoreColumns,
            string strWhere)
        {
            IUpdateable up = _db.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return up.ExecuteCommand() > 0;
        }
        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 10:43:35
        /// 任务编号:中餐
        /// 
        /// 实体
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 条件
        /// 参数
        /// 是否成功
        public bool Update(
            T entity,
            List<string> lstColumns,
            List<string> lstIgnoreColumns,
            string strWhere,
            List lstParameters)
        {
            IUpdateable up = _db.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere, lstParameters);
            }
            return up.ExecuteCommand() > 0;
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-15 10:10:53
        /// 任务编号:中餐
        /// 
        /// entity
        /// 更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 不更新的列,如果为空则不做限制(lstColumns与lstIgnoreColumns互斥)
        /// 条件表达式
        /// 是否成功
        public bool Update(
         T entity,
         List<string> lstColumns,
         List<string> lstIgnoreColumns,
         Expressionbool>> whereExpression)
        {
            IUpdateable up = _db.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(it => lstIgnoreColumns.Contains(it));
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(it => lstColumns.Contains(it));
            }
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            return up.ExecuteCommand() > 0;
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-14 15:40:53
        /// 任务编号:中餐
        /// 
        /// 列,值
        /// 条件
        /// 参数
        /// 是否成功
        public bool Update(
            Dictionary<string, object> lstColumnValues,
            string strWhere,
            List lstParameters)
        {
            IUpdateable up = _db.Updateable(lstColumnValues);
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere, lstParameters);
            }
            return up.ExecuteCommand() > 0;
        }

        /// 
        /// 功能描述:修改数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-14 15:42:27
        /// 任务编号:中餐
        /// 
        /// 列,值
        /// 条件
        /// 是否成功
        public bool Update(Dictionary<string, object> lstColumnValues, Expressionbool>> whereExpression)
        {
            IUpdateable up = _db.Updateable(lstColumnValues);
            if (whereExpression != null)
            {
                up = up.Where(whereExpression);
            }
            return up.ExecuteCommand() > 0;
        }
        #endregion

        #region/// 
        /// 功能描述:数据条数
        /// 作  者:beck.huang
        /// 创建日期:2018-05-25 18:07:00
        /// 任务编号:中餐
        /// 
        /// strWhere
        /// 返回值
        public int SelectCount(string strWhere)
        {
            return _db.Queryable()
                .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere)
                .Count();
        }
        /// 
        /// 功能描述:查询所有数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 14:02:34
        /// 任务编号:中餐
        /// 
        /// 数据列表
        public List Query()
        {
            return _entityDB.GetList();
        }

        /// 
        /// 功能描述:查询数据列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:39:11
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 数据列表
        public List Query(string strWhere)
        {
            return _db.Queryable().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
        }

        /// 
        /// 功能描述:查询数据列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:40:32
        /// 任务编号:中餐
        /// 
        /// whereExpression
        /// 数据列表
        public List Query(Expressionbool>> whereExpression)
        {
            return _entityDB.GetList(whereExpression);
        }
        /// 
        /// 功能描述:查询一个列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:13:55
        /// 任务编号:中餐
        /// 
        /// 条件表达式
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(Expressionbool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToList();
        }

        /// 
        /// 功能描述:查询一个列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:14:54
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(string strWhere, string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
        }

        /// 
        /// 功能描述:查询一个列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:14:54
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
            string strWhere,
            List lstParameters,
            string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).ToList();
        }

        /// 
        /// 功能描述:查询前N条数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:16:09
        /// 任务编号:中餐
        /// 
        /// 条件表达式
        /// 前N条
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
            Expressionbool>> whereExpression,
            int intTop,
            string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList();
        }

        /// 
        /// 功能描述:查询前N条数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:17:14
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 前N条
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
            string strWhere,
            int intTop,
            string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList();
        }

        /// 
        /// 功能描述:查询前N条数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:17:14
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 前N条
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
            string strWhere,
            List lstParameters,
            int intTop,
            string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).Take(intTop).ToList();
        }

        /// 
        /// 功能描述:分页查询
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:27:17
        /// 任务编号:中餐
        /// 
        /// 条件表达式
        /// 页码(下标0)
        /// 页大小
        /// 数据总量
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
            Expressionbool>> whereExpression,
            int intPageIndex,
            int intPageSize,
            ref int intTotalCount,
            string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
        }

        /// 
        /// 功能描述:分页查询
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:29:07
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 页码(下标0)
        /// 页大小
        /// 数据总量
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
          string strWhere,
          int intPageIndex,
          int intPageSize,
          ref int intTotalCount,
          string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
        }

        /// 
        /// 功能描述:分页查询
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:29:07
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 页码(下标0)
        /// 页大小
        /// 数据总量
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List Query(
          string strWhere,
          List lstParameters,
          int intPageIndex,
          int intPageSize,
          ref int intTotalCount,
          string strOrderByFileds)
        {
            return _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).ToPageList(intPageIndex, intPageSize, ref intTotalCount);
        }

        /// 
        /// 功能描述:根据ID查询一条数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:23:21
        /// 任务编号:中餐
        /// 
        /// id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 数据实体
        public T QueryByID(object objId)
        {
            return _db.Queryable().InSingle(objId);
        }
        /// 
        /// 功能描述:根据ID查询一条数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-14 16:58:09
        /// 任务编号:中餐
        /// 
        /// id(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 是否使用缓存
        /// 缓存超时时间(秒)
        /// 数据实体
        public T QueryByID(
            object objId,
            bool blnUseCache = false,
            int cacheDurationInSeconds = 2147483647)
        {
            return _db.Queryable().WithCacheIF(blnUseCache).InSingle(objId);
        }

        /// 
        /// 功能描述:根据ID查询数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 11:23:34
        /// 任务编号:中餐
        /// 
        /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 数据实体列表
        public List QueryByIDs(object[] lstIds)
        {
            return _db.Queryable().In(lstIds).ToList();
        }
        /// 
        /// 功能描述:根据ID查询一条数据
        /// 作  者:beck.huang
        /// 创建日期:2018-05-14 16:58:47
        /// 任务编号:中餐
        /// 
        /// id列表(必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]),如果是联合主键,请使用Where条件
        /// 是否使用缓存
        /// 缓存超时时间(秒)
        /// 数据实体列表
        public List QueryByIDs(
            object[] lstIds,
            bool blnUseCache = false,
            int cacheDurationInSeconds = 2147483647)
        {
            return _db.Queryable().WithCacheIF(blnUseCache).In(lstIds).ToList();
        }


        #region QueryEntity
        /// 
        /// 功能描述:查询一个实体
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:45:37
        /// 任务编号:中餐
        /// 
        /// 条件表达式
        /// 是否使用缓存机制
        /// 缓存过期时长
        /// 实体
        public T QueryEntity(
            Expressionbool>> whereExpression,
            bool blnUseCache = false,
            int cacheDurationInSeconds = 2147483647)
        {
            return _db.Queryable().WithCacheIF(blnUseCache, cacheDurationInSeconds).WhereIF(whereExpression != null, whereExpression).First();
        }

        /// 
        /// 功能描述:查询一个实体
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:45:44
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 是否使用缓存机制
        /// 缓存过期时长
        /// 实体
        public T QueryEntity(
            string strWhere,
            List lstParameters = null,
            bool blnUseCache = false,
            int cacheDurationInSeconds = 2147483647)
        {
            return _db.Queryable().WithCacheIF(blnUseCache, cacheDurationInSeconds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters).First();
        }

        #endregion

        #region QueryList
        /// 
        /// 功能描述:查询一个列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:47:14
        /// 任务编号:中餐
        /// 
        /// 是否使用缓存机制
        /// 缓存过期时长
        /// 返回值
        public List QueryList(bool blnUseCache = false, int cacheDurationInSeconds = 2147483647)
        {
            return _db.Queryable().WithCacheIF(blnUseCache, cacheDurationInSeconds).ToList();
        }

        /// 
        /// 功能描述:查询一个列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:50:32
        /// 任务编号:中餐
        /// 
        /// 条件表达式
        /// 前N条数据
        /// 排序字段,如name asc,age desc
        /// 是否使用缓存机制
        /// 缓存过期时长
        /// 数据列表
        public List QueryList(
            Expressionbool>> whereExpression,
            int? intTop = null,
            string strOrderByFileds = null,
            bool blnUseCache = false,
            int cacheDurationInSeconds = 2147483647)
        {
            ISugarQueryable q = _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression);
            if (intTop.HasValue)
            {
                q = q.Take(intTop.Value);
            }
            return q.WithCacheIF(blnUseCache, cacheDurationInSeconds).ToList();
        }

        /// 
        /// 功能描述:查询一个列表
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:52:17
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 参数
        /// 前N条数据
        /// 排序字段,如name asc,age desc
        /// 是否使用缓存机制
        /// 缓存过期时长
        /// 数据列表
        public List QueryList(
            string strWhere,
            List lstParameters = null,
            int? intTop = null,
            string strOrderByFileds = null,
            bool blnUseCache = false,
            int cacheDurationInSeconds = 2147483647)
        {
            ISugarQueryable q = _db.Queryable().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters);
            if (intTop.HasValue)
            {
                q = q.Take(intTop.Value);
            }
            return q.WithCacheIF(blnUseCache, cacheDurationInSeconds).ToList();
        }
        #endregion

        #region QueryPage
        /// 
        /// 功能描述:分页查询
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:55:06
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 数据总数
        /// 当前页
        /// 页大小
        /// 参数
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List QueryPage(
         string strWhere,
         ref int intTotalCount,
         int intPageIndex = 0,
         int intPageSize = 20,
         List lstParameters = null,
         string strOrderByFileds = null)
        {
            return _db.Queryable()
                .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                .WhereIF(!string.IsNullOrEmpty(strWhere), strWhere, lstParameters)
                .ToPageList(intPageIndex, intPageSize, ref intTotalCount);
        }

        /// 
        /// 功能描述:分页查询
        /// 作  者:beck.huang
        /// 创建日期:2018-05-10 10:55:06
        /// 任务编号:中餐
        /// 
        /// 条件
        /// 数据总数
        /// 当前页
        /// 页大小
        /// 参数
        /// 排序字段,如name asc,age desc
        /// 数据列表
        public List QueryPage(
         Expressionbool>> whereExpression,
         ref int intTotalCount,
         int intPageIndex = 0,
         int intPageSize = 20,
         string strOrderByFileds = null)
        {
            return _db.Queryable()
                .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
                .WhereIF(whereExpression != null, whereExpression)
                .ToPageList(intPageIndex, intPageSize, ref intTotalCount);
        }
        #endregion

        /// 
        /// 功能描述:查询Table
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 18:03:14
        /// 任务编号:中餐
        /// 
        /// strSql
        /// 参数
        /// DataTable
        public DataTable QueryTable(string strSql, SugarParameter[] lstParameters = null)
        {
            return _db.Ado.GetDataTable(strSql, lstParameters);
        }

        /// 
        /// 功能描述:查询DataSet
        /// 作  者:beck.huang
        /// 创建日期:2018-05-16 18:06:05
        /// 任务编号:中餐
        /// 
        /// strSql
        /// 参数
        /// DataSet
        public DataSet QueryDataSet(string strSql, SugarParameter[] lstParameters = null)
        {
            return _db.Ado.GetDataSetAll(strSql, lstParameters);
        }
        #endregion

        #region 事务
        /// 
        /// 功能描述:开始事务
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:49:49
        /// 任务编号:中餐
        /// 
        public void BeginTran()
        {
            _db.Ado.BeginTran();
        }

        /// 
        /// 功能描述:提交事务
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:49:57
        /// 任务编号:中餐
        /// 
        public void CommitTran()
        {
            _db.Ado.CommitTran();
        }

        /// 
        /// 功能描述:回滚事务
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 09:50:01
        /// 任务编号:中餐
        /// 
        public void RollbackTran()
        {
            _db.Ado.RollbackTran();
        }
        #endregion

        #region 其他
        /// 
        /// 功能描述:获取数据库时间
        /// 作  者:beck.huang
        /// 创建日期:2018-05-09 19:41:31
        /// 任务编号:中餐
        /// 
        /// 返回值
        public DateTime GetDBTime()
        {
            return _db.GetDate();
        }

        /// 
        /// 功能描述:清除表缓存
        /// 作  者:beck.huang
        /// 创建日期:2018-05-11 10:15:51
        /// 任务编号:中餐
        /// 
        public void RemoveCache()
        {
            var cacheService = _db.CurrentConnectionConfig.ConfigureExternalServices.DataInfoCacheService;
            string tableName = _db.EntityMaintenance.GetTableName();
            var keys = cacheService.GetAllKey<string>();
            if (keys != null && keys.Count() > 0)
            {
                foreach (var item in keys)
                {
                    if (item.ToLower().Contains("." + tableName.ToLower() + "."))
                    {
                        cacheService.Remove<string>(item);
                    }
                }
            }
        }

        /// 
        /// 功能描述:关闭连接
        /// 作  者:beck.huang
        /// 创建日期:2018-05-28 17:37:16
        /// 任务编号:中餐
        /// 
        public void CloseDB()
        {
            if (_context != null && _context.Db != null)
            {
                _context.Db.Close();
                _context.Db.Dispose();
            }
        }
        #endregion

        #region 事件
        ///// 
        ///// 功能描述:Sql执行完发生
        ///// 作  者:beck.huang
        ///// 创建日期:2018-05-11 10:40:25
        ///// 任务编号:中餐
        ///// 
        ///// sql
        ///// pars
        //public virtual void OnLogExecuted(string sql, SugarParameter[] pars)
        //{

        //}
        ///// 
        ///// 功能描述:Sql执行前发生
        ///// 作  者:beck.huang
        ///// 创建日期:2018-05-11 10:40:25
        ///// 任务编号:中餐
        ///// 
        ///// sql
        ///// pars
        //public virtual void OnLogExecuting(string sql, SugarParameter[] pars)
        //{

        //}
        ///// 
        ///// 功能描述:执行SQL 错误时发生
        ///// 作  者:beck.huang
        ///// 创建日期:2018-05-11 10:40:25
        ///// 任务编号:中餐
        ///// 
        ///// 错误
        //public virtual void OnError(Exception ex)
        //{

        //}

        ///// 
        ///// 功能描述:SQL执行前 可以修改SQL
        ///// 作  者:beck.huang
        ///// 创建日期:2018-05-11 10:47:37
        ///// 任务编号:中餐
        ///// 
        ///// sql
        ///// pars
        ///// 返回值
        //public virtual KeyValuePair OnExecutingChangeSql(string sql, SugarParameter[] pars)
        //{
        //    return new KeyValuePair(sql, pars);
        //}
        #endregion

      
    }
View Code

 HttpRuntimeCache

  1 using SqlSugar;
  2 using System;
  3 using System.Collections;
  4 using System.Collections.Generic;
  5 using System.Linq;
  6 using System.Text;
  7 using System.Web;
  8 using System.Web.Caching;
  9 
 10 namespace Km.DB
 11 {
 12     public class HttpRuntimeCache : ICacheService
 13     {
 14         public void Add(string key, V value)
 15         {
 16             HttpRuntimeCacheHelper.GetInstance().Add(key, value);
 17         }
 18 
 19         public void Add(string key, V value, int cacheDurationInSeconds)
 20         {
 21             HttpRuntimeCacheHelper.GetInstance().Add(key, value, cacheDurationInSeconds);
 22         }
 23 
 24         public bool ContainsKey(string key)
 25         {
 26             return HttpRuntimeCacheHelper.GetInstance().ContainsKey(key);
 27         }
 28 
 29         public V Get(string key)
 30         {
 31             return HttpRuntimeCacheHelper.GetInstance().Get(key);
 32         }
 33 
 34         public IEnumerable<string> GetAllKey()
 35         {
 36             return HttpRuntimeCacheHelper.GetInstance().GetAllKey();
 37         }
 38 
 39         public V GetOrCreate(string cacheKey, Func create, int cacheDurationInSeconds = int.MaxValue)
 40         {
 41             var cacheManager = HttpRuntimeCacheHelper.GetInstance();
 42             if (cacheManager.ContainsKey(cacheKey))
 43             {
 44                 return cacheManager[cacheKey];
 45             }
 46             else
 47             {
 48                 var result = create();
 49                 cacheManager.Add(cacheKey, result, cacheDurationInSeconds);
 50                 return result;
 51             }
 52         }
 53 
 54         public void Remove(string key)
 55         {
 56             HttpRuntimeCacheHelper.GetInstance().Remove(key);
 57         }
 58     }
 59 
 60     internal class HttpRuntimeCacheHelper
 61     {
 62 
 63         #region 全局变量
 64         private static HttpRuntimeCacheHelper _instance = null;
 65         private static readonly object _instanceLock = new object();
 66         #endregion
 67 
 68         #region 构造函数
 69 
 70         private HttpRuntimeCacheHelper() { }
 71         #endregion
 72 
 73         #region  属性
 74         ///          
 75         ///根据key获取value     
 76         ///          
 77         ///       
 78         public V this[string key]
 79         {
 80             get { return (V)HttpRuntime.Cache[CreateKey(key)]; }
 81         }
 82         #endregion
 83 
 84         #region 公共函数
 85 
 86         ///          
 87         /// key是否存在       
 88         ///          
 89         /// key         
 90         ///  ///  存在true 不存在false.        /// ///          
 91         public bool ContainsKey(string key)
 92         {
 93             return HttpRuntime.Cache[CreateKey(key)] != null;
 94         }
 95 
 96         ///          
 97         /// 获取缓存值         
 98         ///          
 99         /// key         
100         ///          
101         public V Get(string key)
102         {
103             return (V)HttpRuntime.Cache.Get(CreateKey(key));
104         }
105 
106         ///          
107         /// 获取实例 (单例模式)       
108         ///          
109         ///          
110         public static HttpRuntimeCacheHelper GetInstance()
111         {
112             if (_instance == null)
113                 lock (_instanceLock)
114                     if (_instance == null)
115                         _instance = new HttpRuntimeCacheHelper();
116             return _instance;
117         }
118 
119         ///          
120         /// 插入缓存(默认20分钟)        
121         ///          
122         ///  key         
123         /// value          
124         public void Add(string key, V value)
125         {
126             Add(key, value, 60 * 20);
127         }
128 
129         ///          
130         /// 插入缓存        
131         ///          
132         ///  key         
133         /// value         
134         /// 过期时间单位秒         
135         public void Add(string key, V value, int cacheDurationInSeconds)
136         {
137             Add(key, value, cacheDurationInSeconds, CacheItemPriority.Default);
138         }
139 
140         ///          
141         /// 插入缓存.         
142         ///          
143         /// key         
144         /// value         
145         /// 过期时间单位秒         
146         /// 缓存项属性         
147         public void Add(string key, V value, int cacheDurationInSeconds, CacheItemPriority priority)
148         {
149             string keyString = CreateKey(key);
150             HttpRuntime.Cache.Insert(keyString, value, null,
151             DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
152         }
153 
154         ///          
155         /// 插入缓存.         
156         ///          
157         /// key         
158         /// value         
159         /// 过期时间单位秒         
160         /// 缓存项属性         
161         public void Add(string key, V value, int
162          cacheDurationInSeconds, CacheDependency dependency, CacheItemPriority priority)
163         {
164             string keyString = CreateKey(key);
165             HttpRuntime.Cache.Insert(keyString, value,
166              dependency, DateTime.Now.AddSeconds(cacheDurationInSeconds), Cache.NoSlidingExpiration, priority, null);
167         }
168 
169         ///          
170         /// 删除缓存         
171         ///          
172         /// key         
173         public void Remove(string key)
174         {
175             HttpRuntime.Cache.Remove(CreateKey(key));
176         }
177 
178         /// 
179         /// 清除所有缓存
180         /// 
181         public void RemoveAll()
182         {
183             System.Web.Caching.Cache cache = HttpRuntime.Cache;
184             IDictionaryEnumerator CacheEnum = cache.GetEnumerator();
185             ArrayList al = new ArrayList();
186             while (CacheEnum.MoveNext())
187             {
188                 al.Add(CacheEnum.Key);
189             }
190             foreach (string key in al)
191             {
192                 cache.Remove(key);
193             }
194         }
195 
196         /// 
197         /// 清除所有包含关键字的缓存
198         /// 
199         /// 关键字
200         public void RemoveAll(Func<string, bool> removeExpression)
201         {
202             System.Web.Caching.Cache _cache = System.Web.HttpRuntime.Cache;
203             var allKeyList = GetAllKey();
204             var delKeyList = allKeyList.Where(removeExpression).ToList();
205             foreach (var key in delKeyList)
206             {
207                 HttpRuntime.Cache.Remove(key); ;
208             }
209         }
210 
211         /// 
212         /// 获取所有缓存key
213         /// 
214         /// 
215         public IEnumerable<string> GetAllKey()
216         {
217             IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
218             while (CacheEnum.MoveNext())
219             {
220                 yield return CacheEnum.Key.ToString();
221             }
222         }
223         #endregion
224 
225         #region 私有函数
226 
227         ///          
228         ///创建KEY   
229         ///          
230         /// Key         
231         ///          
232         private string CreateKey(string key)
233         {
234             return key;
235         }
236         #endregion
237     }
238 }
View Code

 至于为什么要再封装一层,是为了学习成本,如果不封装,整个项目组大家都要学习Sqlsugar,封装了之后,只需要我自己学习就可以了,别人只用我封装的类就可以

你可能感兴趣的:(使用SqlSugar封装的数据层基类)