致敬- 調調-博客园 原来的(net core 使用 SqlSugar)


同时感谢

https://www.programminghunter.com/article/23551893646/ 网站记录了信息,不然这么好的文章代码就要消失了,

同时自己备份一下

原来:https://www.cnblogs.com/oneprice/archive/2018/11/07/9923846.html 文章现在已经被删了

1  /// 
 2     ///  SqlSugar 注入Service的扩展方法
 3     /// 
 4     public static class SqlSugarServiceCollectionExtensions
 5     {
 6         /// 
 7         /// SqlSugar上下文注入
 8         /// 
 9         /// 要注册的上下文的类型
10         /// 
11         /// 
12         /// 用于在容器中注册TSugarClient服务的生命周期
13         /// 
14         public static IServiceCollection AddSqlSugarClient(this IServiceCollection serviceCollection, Action configAction, ServiceLifetime lifetime = ServiceLifetime.Singleton)
15             where TSugarContext : IDbFactory
16         {
17             serviceCollection.AddMemoryCache().AddLogging();
18             serviceCollection.TryAdd(new ServiceDescriptor(typeof(ConnectionConfig), p => ConnectionConfigFactory(p, configAction), lifetime));
19             serviceCollection.Add(new ServiceDescriptor(typeof(ConnectionConfig), p => ConnectionConfigFactory(p, configAction), lifetime));
20             serviceCollection.TryAdd(new ServiceDescriptor(typeof(TSugarContext), typeof(TSugarContext), lifetime));
21             return serviceCollection;
22         }
23 
24         private static ConnectionConfig ConnectionConfigFactory(IServiceProvider applicationServiceProvider, Action configAction)
25         {
26             var config = new ConnectionConfig();
27             configAction.Invoke(applicationServiceProvider, config);
28             return config;
29         }
30     }
1 public interface IDbFactory
2     {
3         SqlSugarClient GetDbContext(Action onErrorEvent);
4         SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent);
5         SqlSugarClient GetDbContext(Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent);
6         SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent = null, Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent = null, Action onErrorEvent = null);
7     }
1 public class DbFactory : IDbFactory
 2     {
 3         private readonly ILogger _logger;
 4         private readonly ConnectionConfig _config;
 5 
 6         public DbFactory(ConnectionConfig config, ILogger logger)
 7         {
 8             this._logger = logger;
 9             this._config = config;
10         }
11 
12         public SqlSugarClient GetDbContext(Action onErrorEvent) => GetDbContext(null, null, onErrorEvent);
13         public SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent) => GetDbContext(onExecutedEvent);
14         public SqlSugarClient GetDbContext(Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent) => GetDbContext(null, onExecutingChangeSqlEvent);
15         public SqlSugarClient GetDbContext(Action<string, SugarParameter[]> onExecutedEvent = null, Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> onExecutingChangeSqlEvent = null, Action onErrorEvent = null)
16         {
17             SqlSugarClient db = new SqlSugarClient(_config)
18             {
19                 Aop =
20                  {
21                         OnExecutingChangeSql = onExecutingChangeSqlEvent,
22                         OnError = onErrorEvent ?? ((Exception ex) => { this._logger.LogError(ex, "ExecuteSql Error"); }),
23                         OnLogExecuted =onExecutedEvent?? ((string sql, SugarParameter[] pars) =>
24                         {
25                             var keyDic = new KeyValuePair<string, SugarParameter[]>(sql, pars);
26                             this._logger.LogInformation($"ExecuteSql:【{keyDic.ToJson()}】");
27                         })
28                  }
29             };
30             return db;
31         }
32     }
DbFactory
public interface IRepository
    {

    }
1  public class Repository : IRepository where TFactory : IDbFactory where TIRepository : IRepository
 2     {
 3         protected readonly ILogger Log;
 4         protected readonly TFactory Factory;
 5         protected readonly TIRepository DbRepository;
 6         protected SqlSugarClient DbContext => this.Factory.GetDbContext();
 7 
 8         public Repository(TFactory factory) => Factory = factory;
 9         public Repository(TFactory factory, ILogger logger) : this(factory) => Log = logger;
10         public Repository(TFactory factory, TIRepository repository) : this(factory) => DbRepository = repository;
11         public Repository(TFactory factory, TIRepository repository, ILogger logger) : this(factory, repository) => Log = logger;
12     }
13 
14     public class Repository : IRepository where TFactory : IDbFactory
15     {
16         protected readonly ILogger Log;
17         protected readonly TFactory Factory;
18         protected SqlSugarClient DbContext => this.Factory.GetDbContext();
19 
20         public Repository(TFactory factory) => Factory = factory;
21         public Repository(TFactory factory, ILogger logger) : this(factory) => Log = logger;
22     }
1   public static class SugarFactoryExtensions
  2     {
  3 
  4         #region 根据主键获取实体对象
  5 
  6         /// 
  7         /// 根据主键获取实体对象
  8         /// 
  9         /// 数据源类型
 10         /// 
 11         /// 
 12         /// 
 13         public static TSource GetById(this SqlSugarClient db, dynamic id) where TSource : EntityBase, new()
 14         {
 15             return db.Queryable().InSingle(id);
 16         }
 17 
 18         /// 
 19         /// 根据主键获取实体对象
 20         /// 
 21         /// 数据源类型
 22         /// 数据源映射类型
 23         /// 
 24         /// 
 25         /// 
 26         public static TMap GetById(this SqlSugarClient db, dynamic id) where TSource : EntityBase, new()
 27         {
 28             TSource model = db.Queryable().InSingle(id);
 29             return model.Map();
 30         }
 31 
 32         #endregion
 33 
 34         #region 根据Linq表达式条件获取单个实体对象
 35 
 36         /// 
 37         /// 根据条件获取单个实体对象
 38         /// 
 39         /// 数据源类型
 40         /// 
 41         /// 
 42         /// 
 43         public static TSource Get(this SqlSugarClient db, Expressionbool>> whereExp) where TSource : EntityBase, new()
 44         {
 45             return db.Queryable().Where(whereExp).Single();
 46         }
 47 
 48         /// 
 49         /// 根据条件获取单个实体对象
 50         /// 
 51         /// 数据源类型
 52         /// 数据源映射类型
 53         /// 
 54         /// 条件表达式
 55         /// 
 56         public static TMap Get(this SqlSugarClient db, Expressionbool>> whereExp) where TSource : EntityBase, new()
 57         {
 58             TSource model = db.Queryable().Where(whereExp).Single();
 59             return model.Map();
 60         }
 61 
 62         #endregion
 63 
 64         #region 获取所有实体列表
 65 
 66         /// 
 67         /// 获取所有实体列表
 68         /// 
 69         /// 数据源类型
 70         /// 
 71         /// 
 72         public static List GetList(this SqlSugarClient db) where TSource : EntityBase, new()
 73         {
 74             return db.Queryable().ToList();
 75         }
 76 
 77         /// 
 78         /// 获取实体列表
 79         /// 
 80         /// 数据源类型
 81         /// 数据源映射类型
 82         /// 
 83         /// 
 84         public static List GetList(this SqlSugarClient db) where TSource : EntityBase, new()
 85         {
 86             var result = db.Queryable().ToList();
 87             return result.Map, List>();
 88         }
 89 
 90         #endregion
 91 
 92         #region 根据Linq表达式条件获取列表
 93 
 94         /// 
 95         /// 根据条件获取实体列表
 96         /// 
 97         /// 数据源类型
 98         /// 
 99         /// 条件表达式
100         /// 
101         public static List GetList(this SqlSugarClient db, Expressionbool>> whereExp) where TSource : EntityBase, new()
102         {
103             return db.Queryable().Where(whereExp).ToList();
104         }
105 
106         /// 
107         /// 根据条件获取实体列表
108         /// 
109         /// 数据源类型
110         /// 数据源映射类型
111         /// 
112         /// 条件表达式
113         /// 
114         public static List GetList(this SqlSugarClient db, Expressionbool>> whereExp) where TSource : EntityBase, new()
115         {
116             var result = db.Queryable().Where(whereExp).ToList();
117             return result.Map, List>();
118         }
119 
120         #endregion
121 
122         #region 根据Sugar条件获取列表
123 
124         /// 
125         /// 根据条件获取实体列表
126         /// 
127         /// 
128         /// 
129         /// Sugar调价表达式集合
130         /// 
131         public static List GetList(this SqlSugarClient db, List conditionals) where TSource : EntityBase, new()
132         {
133             return db.Queryable().Where(conditionals).ToList();
134         }
135 
136         /// 
137         /// 根据条件获取实体列表
138         /// 
139         /// 数据源类型
140         /// 数据源映射类型
141         /// 
142         /// Sugar调价表达式集合
143         /// 
144         public static List GetList(this SqlSugarClient db, List conditionals) where TSource : EntityBase, new()
145         {
146             var result = db.Queryable().Where(conditionals).ToList();
147             return result.Map, List>();
148         }
149 
150         #endregion
151 
152         #region 是否包含某个元素
153         /// 
154         /// 是否包含某个元素
155         /// 
156         /// 
157         /// 
158         /// 条件表达式
159         /// 
160         public static bool Exist(this SqlSugarClient db, Expressionbool>> whereExp) where TSource : EntityBase, new()
161         {
162             return db.Queryable().Where(whereExp).Any();
163         }
164         #endregion
165 
166         #region 新增实体对象
167         /// 
168         /// 新增实体对象
169         /// 
170         /// 
171         /// 
172         /// 
173         /// 
174         public static bool Insert(this SqlSugarClient db, TSource insertObj) where TSource : EntityBase, new()
175         {
176             return db.Insertable(insertObj).ExecuteCommand() > 0;
177         }
178 
179         /// 
180         /// 新增实体对象
181         /// 
182         /// 
183         /// 
184         /// 
185         /// 
186         /// 
187         public static bool Insert(this SqlSugarClient db, TSource insertDto) where TMap : EntityBase, new()
188         {
189             var entity = insertDto.Map();
190             return db.Insertable(entity).ExecuteCommand() > 0;
191         }
192         #endregion
193 
194         #region 批量新增实体对象
195         /// 
196         /// 批量新增实体对象
197         /// 
198         /// 
199         /// 
200         /// 
201         /// 
202         public static bool InsertRange(this SqlSugarClient db, List insertObjs) where TSource : EntityBase, new()
203         {
204             return db.Insertable(insertObjs).ExecuteCommand() > 0;
205         }
206 
207         /// 
208         /// 批量新增实体对象
209         /// 
210         /// 
211         /// 
212         /// 
213         /// 
214         /// 
215         public static bool InsertRange(this SqlSugarClient db, List insertObjs) where TMap : EntityBase, new()
216         {
217             var entitys = insertObjs.Map, List>();
218             return db.Insertable(entitys).ExecuteCommand() > 0;
219         }
220         #endregion
221 
222         #region 更新单个实体对象
223         /// 
224         /// 更新单个实体对象
225         /// 
226         /// 
227         /// 
228         /// 
229         /// 
230         public static bool Update(this SqlSugarClient db, TSource updateObj) where TSource : EntityBase, new()
231         {
232             return db.Updateable(updateObj).ExecuteCommand() > 0;
233         }
234         #endregion
235 
236         #region 根据条件批量更新实体指定列
237         /// 
238         /// 根据条件批量更新实体指定列
239         /// 
240         /// 
241         /// 
242         /// 需要更新的列
243         /// 条件表达式
244         /// 
245         public static bool Update(this SqlSugarClient db, Expression> columns, Expressionbool>> whereExp) where TSource : EntityBase, new()
246         {
247             return db.Updateable().UpdateColumns(columns).Where(whereExp).ExecuteCommand() > 0;
248         }
249         #endregion
250 
251         #region 物理删除实体对象
252 
253         /// 
254         /// 物理删除实体对象
255         /// 
256         /// 
257         /// 
258         /// 
259         /// 
260         public static bool Delete(this SqlSugarClient db, TSource deleteObj) where TSource : EntityBase, new()
261         {
262             return db.Deleteable().Where(deleteObj).ExecuteCommand() > 0;
263         }
264 
265         /// 
266         /// 物理删除实体对象
267         /// 
268         /// 
269         /// 
270         /// 条件表达式
271         /// 
272         public static bool Delete(this SqlSugarClient db, Expressionbool>> whereExp) where TSource : EntityBase, new()
273         {
274             return db.Deleteable().Where(whereExp).ExecuteCommand() > 0;
275         }
276 
277         /// 
278         /// 根据主键物理删除实体对象
279         /// 
280         /// 
281         /// 
282         /// 
283         /// 
284         public static bool DeleteById(this SqlSugarClient db, dynamic id) where TSource : EntityBase, new()
285         {
286             return db.Deleteable().In(id).ExecuteCommand() > 0;
287         }
288 
289         /// 
290         /// 根据主键批量物理删除实体集合
291         /// 
292         /// 
293         /// 
294         /// 
295         /// 
296         public static bool DeleteByIds(this SqlSugarClient db, dynamic[] ids) where TSource : EntityBase, new()
297         {
298             return db.Deleteable().In(ids).ExecuteCommand() > 0;
299         }
300 
301         #endregion
302 
303         #region 分页查询
304 
305         /// 
306         /// 获取分页列表【页码,每页条数】
307         /// 
308         /// 数据源类型
309         /// 
310         /// 页码(从0开始)
311         /// 每页条数
312         /// 
313         public static IPagedList GetPageList(this SqlSugarClient db, int pageIndex, int pageSize) where TSource : EntityBase, new()
314         {
315             int count = 0;
316             var result = db.Queryable().ToPageList(pageIndex, pageSize, ref count);
317             return new PagedList(result, pageIndex, pageSize, count);
318         }
319 
320         /// 
321         /// 获取分页列表【页码,每页条数】
322         /// 
323         /// 数据源类型
324         /// 数据源映射类型
325         /// 
326         /// 页码(从0开始)
327         /// 每页条数
328         /// 
329         public static IPagedList GetPageList(this SqlSugarClient db, int pageIndex, int pageSize) where TSource : EntityBase, new()
330         {
331             int count = 0;
332             var result = db.Queryable().ToPageList(pageIndex, pageSize, ref count);
333             var pageResult = new PagedList(result, pageIndex, pageSize, count);
334             return pageResult.Map();
335         }
336 
337         #endregion
338 
339         #region 分页查询(排序)
340 
341         /// 
342         /// 获取分页列表【排序,页码,每页条数】
343         /// 
344         /// 数据源类型
345         /// 
346         /// 排序表达式
347         /// 排序类型
348         /// 页码(从0开始)
349         /// 每页条数
350         /// 
351         public static IPagedList GetPageList(this SqlSugarClient db, Expressionobject>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
352         {
353             int count = 0;
354             var result = db.Queryable().OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
355             return new PagedList(result, pageIndex, pageSize, count);
356         }
357 
358         /// 
359         /// 获取分页列表【排序,页码,每页条数】
360         /// 
361         /// 数据源类型
362         /// 数据源映射类型
363         /// 
364         /// 排序表达式
365         /// 排序类型
366         /// 页码(从0开始)
367         /// 每页条数
368         /// 
369         public static IPagedList GetPageList(this SqlSugarClient db, Expressionobject>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
370         {
371             int count = 0;
372             var result = db.Queryable().OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
373             var pageResult = new PagedList(result, pageIndex, pageSize, count);
374             return pageResult.Map();
375         }
376 
377         #endregion
378 
379         #region 分页查询(Linq表达式条件)
380 
381         /// 
382         /// 获取分页列表【Linq表达式条件,页码,每页条数】
383         /// 
384         /// 数据源类型
385         /// 
386         /// Linq表达式条件
387         /// 页码(从0开始)
388         /// 每页条数
389         /// 
390         public static IPagedList GetPageList(this SqlSugarClient db, Expressionbool>> whereExp, int pageIndex, int pageSize) where TSource : EntityBase, new()
391         {
392             int count = 0;
393             var result = db.Queryable().Where(whereExp).ToPageList(pageIndex, pageSize, ref count);
394             return new PagedList(result, pageIndex, pageSize, count);
395         }
396 
397         /// 
398         /// 获取分页列表【Linq表达式条件,页码,每页条数】
399         /// 
400         /// 数据源类型
401         /// 数据源映射类型
402         /// 
403         /// Linq表达式条件
404         /// 页码(从0开始)
405         /// 每页条数
406         /// 
407         public static IPagedList GetPageList(this SqlSugarClient db, Expressionbool>> whereExp, int pageIndex, int pageSize) where TSource : EntityBase, new()
408         {
409             int count = 0;
410             var result = db.Queryable().Where(whereExp).ToPageList(pageIndex, pageSize, ref count);
411             var pageResult = new PagedList(result, pageIndex, pageSize, count);
412             return pageResult.Map();
413         }
414 
415         #endregion
416 
417         #region 分页查询(Linq表达式条件,排序)
418 
419         /// 
420         /// 获取分页列表【Linq表达式条件,排序,页码,每页条数】
421         /// 
422         /// 数据源类型
423         /// 
424         /// Linq表达式条件
425         /// 排序表达式
426         /// 排序类型
427         /// 页码(从0开始)
428         /// 每页条数
429         /// 
430         public static IPagedList GetPageList(this SqlSugarClient db, Expressionbool>> whereExp, Expressionobject>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
431         {
432             int count = 0;
433             var result = db.Queryable().Where(whereExp).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
434             return new PagedList(result, pageIndex, pageSize, count);
435         }
436 
437         /// 
438         ///  获取分页列表【Linq表达式条件,排序,页码,每页条数】
439         /// 
440         /// 数据源类型
441         /// 数据源映射类型
442         /// 
443         /// Linq表达式条件
444         /// 排序表达式
445         /// 排序类型
446         /// 页码(从0开始)
447         /// 每页条数
448         /// 
449         public static IPagedList GetPageList(this SqlSugarClient db, Expressionbool>> whereExp, Expressionobject>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
450         {
451             int count = 0;
452             var result = db.Queryable().Where(whereExp).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
453             var pageResult = new PagedList(result, pageIndex, pageSize, count);
454             return pageResult.Map();
455         }
456 
457         #endregion
458 
459         #region 分页查询(Sugar条件)
460 
461         /// 
462         /// 获取分页列表【Sugar表达式条件,页码,每页条数】
463         /// 
464         /// 数据源类型
465         /// 
466         /// Sugar条件表达式集合
467         /// 页码(从0开始)
468         /// 每页条数
469         /// 
470         public static IPagedList GetPageList(this SqlSugarClient db, List conditionals, int pageIndex, int pageSize) where TSource : EntityBase, new()
471         {
472             int count = 0;
473             var result = db.Queryable().Where(conditionals).ToPageList(pageIndex, pageSize, ref count);
474             return new PagedList(result, pageIndex, pageSize, count);
475         }
476 
477         /// 
478         /// 获取分页列表【Sugar表达式条件,页码,每页条数】
479         /// 
480         /// 数据源类型
481         /// 数据源映射类型
482         /// 
483         /// Sugar条件表达式集合
484         /// 页码(从0开始)
485         /// 每页条数
486         /// 
487         public static IPagedList GetPageList(this SqlSugarClient db, List conditionals, int pageIndex, int pageSize) where TSource : EntityBase, new()
488         {
489             int count = 0;
490             var result = db.Queryable().Where(conditionals).ToPageList(pageIndex, pageSize, ref count);
491             var pageResult = new PagedList(result, pageIndex, pageSize, count);
492             return pageResult.Map();
493         }
494 
495         #endregion
496 
497         #region 分页查询(Sugar条件,排序)
498 
499         /// 
500         ///  获取分页列表【Sugar表达式条件,排序,页码,每页条数】
501         /// 
502         /// 
503         /// 
504         /// Sugar条件表达式集合
505         /// 排序表达式
506         /// 排序类型
507         /// 页码(从0开始)
508         /// 每页条数
509         /// 
510         public static IPagedList GetPageList(this SqlSugarClient db, List conditionals, Expressionobject>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
511         {
512             int count = 0;
513             var result = db.Queryable().Where(conditionals).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
514             return new PagedList(result, pageIndex, pageSize, count);
515         }
516 
517         /// 
518         ///  获取分页列表【Sugar表达式条件,排序,页码,每页条数】
519         /// 
520         /// 
521         /// 
522         /// Sugar条件表达式集合
523         /// 排序表达式
524         /// 排序类型
525         /// 页码(从0开始)
526         /// 每页条数
527         /// 
528         public static IPagedList GetPageList(this SqlSugarClient db, List conditionals, Expressionobject>> orderExp, OrderByType orderType, int pageIndex, int pageSize) where TSource : EntityBase, new()
529         {
530             int count = 0;
531             var result = db.Queryable().Where(conditionals).OrderBy(orderExp, orderType).ToPageList(pageIndex, pageSize, ref count);
532             var pageResult = new PagedList(result, pageIndex, pageSize, count);
533             return pageResult.Map();
534         }
535 
536         #endregion
537 
538         #region 分页查询 (扩展条件构造实体,默认排序列,默认排序方式)
539         /// 
540         /// 分页查询 (扩展条件构造实体,默认排序列,默认排序方式)
541         /// 
542         /// 
543         /// 
544         /// 
545         /// 
546         /// 
547         /// 
548         /// 
549         public static IPagedList GetPageList(this SqlSugarClient db, QueryCollection query, Expressionobject>> defaultSort, OrderByType defaultSortType) where TSource : EntityBase, new()
550         {
551             int count = 0;
552             List conditionals = query.ConditionItems.ExamineConditional();
553             Expressionobject>> sort = query.SortLambdaobject>(defaultSort, defaultSortType, out var sortType);
554             var result = db.Queryable().Where(conditionals).OrderBy(sort, sortType).ToPageList(query.PageIndex, query.PageSize, ref count);
555             var pageResult = new PagedList(result, query.PageIndex, query.PageSize, count);
556             return pageResult.Map();
557         }
558         #endregion
559 
560         #region 分页查询 (扩展条件构造实体,默认排序列,默认排序方式,Linq表达式条件)
561         /// 
562         /// 分页查询 (扩展条件构造实体,默认排序列,默认排序方式,Linq表达式条件)
563         /// 
564         /// 
565         /// 
566         /// 
567         /// 
568         /// 
569         /// 
570         /// 
571         /// 
572         public static IPagedList GetPageList(this SqlSugarClient db, QueryCollection query, Expressionobject>> defaultSort, OrderByType defaultSortType, Expressionbool>> whereExp) where TSource : EntityBase, new()
573         {
574             int count = 0;
575             List conditionals = query.ConditionItems.ExamineConditional();
576             Expressionobject>> sort = query.SortLambdaobject>(defaultSort, defaultSortType, out var sortType);
577             var result = db.Queryable().Where(whereExp).Where(conditionals).OrderBy(sort, sortType).ToPageList(query.PageIndex, query.PageSize, ref count);
578             var pageResult = new PagedList(result, query.PageIndex, query.PageSize, count);
579             return pageResult.Map();
580         }
581         #endregion
582     }
1             services.AddSqlSugarClient((sp, op) =>
2             {
3                 op.ConnectionString = sp.GetService().GetConnectionString("lucy");
4                 op.DbType = DbType.MySql;
5                 op.IsAutoCloseConnection = true;
6                 op.InitKeyType = InitKeyType.Attribute;
7                 op.IsShardSameThread = true;
8             });
1     //如果数据操作简单,直接在业务层使用
 2     public class UsersService : Repository, IUsersService
 3     {
 4         public UsersService(DbFactory factory, IUsersRepository) : base(factory)
 5         {
 6 
 7         }
 8 
 9 
10         public async Task TestMethod()
11         {
12             //获取数据库上下文
13             //第一种  
14             DbContext.Insert(new Users());
15             //第二种
16             using (var db = Factory.GetDbContext())
17             {
18                 db.Insert(new Users());
19                 db.Update(new Users());
20             }
21             //数据操作繁琐的放到自定义的IUsersRepository中
22             await DbRepository.TestAddAsync();
23         }
24 
25     }
1  public class UsersRepository : Repository, IUsersRepository
 2     {
 3         public UsersRepository(DbFactory factory) : base(factory)
 4         {
 5 
 6         }
 7 
 8         public async Task<bool> TestAddAsync()
 9         {
10             //这里获取数据库上下文,与业务层一致
11 
12             DbContext.Insert(new Users());
13 
14             using (var db = Factory.GetDbContext())
15             {
16                 db.Insert(new Users());
17                 db.Update(new Users());
18             }
19             return await Task.FromResult(true);
20         }
21     }