欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

net core 使用 SqlSugar

程序员文章站 2022-05-14 08:26:25
1 /// 2 /// SqlSugar 注入Service的扩展方法 3 /// 4 public static class SqlSugarServiceCollectionExtensions 5 { 6 /// 7 /// Sql ......
net core 使用 SqlSugar
 1  /// <summary>
 2     ///  sqlsugar 注入service的扩展方法
 3     /// </summary>
 4     public static class sqlsugarservicecollectionextensions
 5     {
 6         /// <summary>
 7         /// sqlsugar上下文注入
 8         /// </summary>
 9         /// <typeparam name="tsugarcontext">要注册的上下文的类型</typeparam>
10         /// <param name="servicecollection"></param>
11         /// <param name="configaction"></param>
12         /// <param name="lifetime">用于在容器中注册tsugarclient服务的生命周期</param>
13         /// <returns></returns>
14         public static iservicecollection addsqlsugarclient<tsugarcontext>(this iservicecollection servicecollection, action<iserviceprovider, connectionconfig> 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<iserviceprovider, connectionconfig> configaction)
25         {
26             var config = new connectionconfig();
27             configaction.invoke(applicationserviceprovider, config);
28             return config;
29         }
30     }
注入扩展
net core 使用 SqlSugar
1 public interface idbfactory
2     {
3         sqlsugarclient getdbcontext(action<exception> 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<exception> onerrorevent = null);
7     }
idbfactory
net core 使用 SqlSugar
 1 public class dbfactory : idbfactory
 2     {
 3         private readonly ilogger _logger;
 4         private readonly connectionconfig _config;
 5 
 6         public dbfactory(connectionconfig config, ilogger<dbfactory> logger)
 7         {
 8             this._logger = logger;
 9             this._config = config;
10         }
11 
12         public sqlsugarclient getdbcontext(action<exception> 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<exception> 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
net core 使用 SqlSugar
    public interface irepository
    {

    }
irepository
net core 使用 SqlSugar
 1  public class repository<tfactory, tirepository> : 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<tfactory> : 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     }
repository
net core 使用 SqlSugar
  1   public static class sugarfactoryextensions
  2     {
  3 
  4         #region 根据主键获取实体对象
  5 
  6         /// <summary>
  7         /// 根据主键获取实体对象
  8         /// </summary>
  9         /// <typeparam name="tsource">数据源类型</typeparam>
 10         /// <param name="db"></param>
 11         /// <param name="id"></param>
 12         /// <returns></returns>
 13         public static tsource getbyid<tsource>(this sqlsugarclient db, dynamic id) where tsource : entitybase, new()
 14         {
 15             return db.queryable<tsource>().insingle(id);
 16         }
 17 
 18         /// <summary>
 19         /// 根据主键获取实体对象
 20         /// </summary>
 21         /// <typeparam name="tsource">数据源类型</typeparam>
 22         /// <typeparam name="tmap">数据源映射类型</typeparam>
 23         /// <param name="db"></param>
 24         /// <param name="id"></param>
 25         /// <returns></returns>
 26         public static tmap getbyid<tsource, tmap>(this sqlsugarclient db, dynamic id) where tsource : entitybase, new()
 27         {
 28             tsource model = db.queryable<tsource>().insingle(id);
 29             return model.map<tsource, tmap>();
 30         }
 31 
 32         #endregion
 33 
 34         #region 根据linq表达式条件获取单个实体对象
 35 
 36         /// <summary>
 37         /// 根据条件获取单个实体对象
 38         /// </summary>
 39         /// <typeparam name="tsource">数据源类型</typeparam>
 40         /// <param name="db"></param>
 41         /// <param name="whereexp"></param>
 42         /// <returns></returns>
 43         public static tsource get<tsource>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
 44         {
 45             return db.queryable<tsource>().where(whereexp).single();
 46         }
 47 
 48         /// <summary>
 49         /// 根据条件获取单个实体对象
 50         /// </summary>
 51         /// <typeparam name="tsource">数据源类型</typeparam>
 52         /// <typeparam name="tmap">数据源映射类型</typeparam>
 53         /// <param name="db"></param>
 54         /// <param name="whereexp">条件表达式</param>
 55         /// <returns></returns>
 56         public static tmap get<tsource, tmap>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
 57         {
 58             tsource model = db.queryable<tsource>().where(whereexp).single();
 59             return model.map<tsource, tmap>();
 60         }
 61 
 62         #endregion
 63 
 64         #region 获取所有实体列表
 65 
 66         /// <summary>
 67         /// 获取所有实体列表
 68         /// </summary>
 69         /// <typeparam name="tsource">数据源类型</typeparam>
 70         /// <param name="db"></param>
 71         /// <returns></returns>
 72         public static list<tsource> getlist<tsource>(this sqlsugarclient db) where tsource : entitybase, new()
 73         {
 74             return db.queryable<tsource>().tolist();
 75         }
 76 
 77         /// <summary>
 78         /// 获取实体列表
 79         /// </summary>
 80         /// <typeparam name="tsource">数据源类型</typeparam>
 81         /// <typeparam name="tmap">数据源映射类型</typeparam>
 82         /// <param name="db"></param>
 83         /// <returns></returns>
 84         public static list<tmap> getlist<tsource, tmap>(this sqlsugarclient db) where tsource : entitybase, new()
 85         {
 86             var result = db.queryable<tsource>().tolist();
 87             return result.map<list<tsource>, list<tmap>>();
 88         }
 89 
 90         #endregion
 91 
 92         #region 根据linq表达式条件获取列表
 93 
 94         /// <summary>
 95         /// 根据条件获取实体列表
 96         /// </summary>
 97         /// <typeparam name="tsource">数据源类型</typeparam>
 98         /// <param name="db"></param>
 99         /// <param name="whereexp">条件表达式</param>
100         /// <returns></returns>
101         public static list<tsource> getlist<tsource>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
102         {
103             return db.queryable<tsource>().where(whereexp).tolist();
104         }
105 
106         /// <summary>
107         /// 根据条件获取实体列表
108         /// </summary>
109         /// <typeparam name="tsource">数据源类型</typeparam>
110         /// <typeparam name="tmap">数据源映射类型</typeparam>
111         /// <param name="db"></param>
112         /// <param name="whereexp">条件表达式</param>
113         /// <returns></returns>
114         public static list<tmap> getlist<tsource, tmap>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
115         {
116             var result = db.queryable<tsource>().where(whereexp).tolist();
117             return result.map<list<tsource>, list<tmap>>();
118         }
119 
120         #endregion
121 
122         #region 根据sugar条件获取列表
123 
124         /// <summary>
125         /// 根据条件获取实体列表
126         /// </summary>
127         /// <typeparam name="tsource"></typeparam>
128         /// <param name="db"></param>
129         /// <param name="conditionals">sugar调价表达式集合</param>
130         /// <returns></returns>
131         public static list<tsource> getlist<tsource>(this sqlsugarclient db, list<iconditionalmodel> conditionals) where tsource : entitybase, new()
132         {
133             return db.queryable<tsource>().where(conditionals).tolist();
134         }
135 
136         /// <summary>
137         /// 根据条件获取实体列表
138         /// </summary>
139         /// <typeparam name="tsource">数据源类型</typeparam>
140         /// <typeparam name="tmap">数据源映射类型</typeparam>
141         /// <param name="db"></param>
142         /// <param name="conditionals">sugar调价表达式集合</param>
143         /// <returns></returns>
144         public static list<tmap> getlist<tsource, tmap>(this sqlsugarclient db, list<iconditionalmodel> conditionals) where tsource : entitybase, new()
145         {
146             var result = db.queryable<tsource>().where(conditionals).tolist();
147             return result.map<list<tsource>, list<tmap>>();
148         }
149 
150         #endregion
151 
152         #region 是否包含某个元素
153         /// <summary>
154         /// 是否包含某个元素
155         /// </summary>
156         /// <typeparam name="tsource"></typeparam>
157         /// <param name="db"></param>
158         /// <param name="whereexp">条件表达式</param>
159         /// <returns></returns>
160         public static bool exist<tsource>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
161         {
162             return db.queryable<tsource>().where(whereexp).any();
163         }
164         #endregion
165 
166         #region 新增实体对象
167         /// <summary>
168         /// 新增实体对象
169         /// </summary>
170         /// <typeparam name="tsource"></typeparam>
171         /// <param name="db"></param>
172         /// <param name="insertobj"></param>
173         /// <returns></returns>
174         public static bool insert<tsource>(this sqlsugarclient db, tsource insertobj) where tsource : entitybase, new()
175         {
176             return db.insertable(insertobj).executecommand() > 0;
177         }
178 
179         /// <summary>
180         /// 新增实体对象
181         /// </summary>
182         /// <typeparam name="tsource"></typeparam>
183         /// <typeparam name="tmap"></typeparam>
184         /// <param name="db"></param>
185         /// <param name="insertdto"></param>
186         /// <returns></returns>
187         public static bool insert<tsource, tmap>(this sqlsugarclient db, tsource insertdto) where tmap : entitybase, new()
188         {
189             var entity = insertdto.map<tsource, tmap>();
190             return db.insertable(entity).executecommand() > 0;
191         }
192         #endregion
193 
194         #region 批量新增实体对象
195         /// <summary>
196         /// 批量新增实体对象
197         /// </summary>
198         /// <typeparam name="tsource"></typeparam>
199         /// <param name="db"></param>
200         /// <param name="insertobjs"></param>
201         /// <returns></returns>
202         public static bool insertrange<tsource>(this sqlsugarclient db, list<tsource> insertobjs) where tsource : entitybase, new()
203         {
204             return db.insertable(insertobjs).executecommand() > 0;
205         }
206 
207         /// <summary>
208         /// 批量新增实体对象
209         /// </summary>
210         /// <typeparam name="tsource"></typeparam>
211         /// <typeparam name="tmap"></typeparam>
212         /// <param name="db"></param>
213         /// <param name="insertobjs"></param>
214         /// <returns></returns>
215         public static bool insertrange<tsource, tmap>(this sqlsugarclient db, list<tsource> insertobjs) where tmap : entitybase, new()
216         {
217             var entitys = insertobjs.map<list<tsource>, list<tmap>>();
218             return db.insertable(entitys).executecommand() > 0;
219         }
220         #endregion
221 
222         #region 更新单个实体对象
223         /// <summary>
224         /// 更新单个实体对象
225         /// </summary>
226         /// <typeparam name="tsource"></typeparam>
227         /// <param name="db"></param>
228         /// <param name="updateobj"></param>
229         /// <returns></returns>
230         public static bool update<tsource>(this sqlsugarclient db, tsource updateobj) where tsource : entitybase, new()
231         {
232             return db.updateable(updateobj).executecommand() > 0;
233         }
234         #endregion
235 
236         #region 根据条件批量更新实体指定列
237         /// <summary>
238         /// 根据条件批量更新实体指定列
239         /// </summary>
240         /// <typeparam name="tsource"></typeparam>
241         /// <param name="db"></param>
242         /// <param name="columns">需要更新的列</param>
243         /// <param name="whereexp">条件表达式</param>
244         /// <returns></returns>
245         public static bool update<tsource>(this sqlsugarclient db, expression<func<tsource, tsource>> columns, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
246         {
247             return db.updateable<tsource>().updatecolumns(columns).where(whereexp).executecommand() > 0;
248         }
249         #endregion
250 
251         #region 物理删除实体对象
252 
253         /// <summary>
254         /// 物理删除实体对象
255         /// </summary>
256         /// <typeparam name="tsource"></typeparam>
257         /// <param name="db"></param>
258         /// <param name="deleteobj"></param>
259         /// <returns></returns>
260         public static bool delete<tsource>(this sqlsugarclient db, tsource deleteobj) where tsource : entitybase, new()
261         {
262             return db.deleteable<tsource>().where(deleteobj).executecommand() > 0;
263         }
264 
265         /// <summary>
266         /// 物理删除实体对象
267         /// </summary>
268         /// <typeparam name="tsource"></typeparam>
269         /// <param name="db"></param>
270         /// <param name="whereexp">条件表达式</param>
271         /// <returns></returns>
272         public static bool delete<tsource>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
273         {
274             return db.deleteable<tsource>().where(whereexp).executecommand() > 0;
275         }
276 
277         /// <summary>
278         /// 根据主键物理删除实体对象
279         /// </summary>
280         /// <typeparam name="tsource"></typeparam>
281         /// <param name="db"></param>
282         /// <param name="id"></param>
283         /// <returns></returns>
284         public static bool deletebyid<tsource>(this sqlsugarclient db, dynamic id) where tsource : entitybase, new()
285         {
286             return db.deleteable<tsource>().in(id).executecommand() > 0;
287         }
288 
289         /// <summary>
290         /// 根据主键批量物理删除实体集合
291         /// </summary>
292         /// <typeparam name="tsource"></typeparam>
293         /// <param name="db"></param>
294         /// <param name="ids"></param>
295         /// <returns></returns>
296         public static bool deletebyids<tsource>(this sqlsugarclient db, dynamic[] ids) where tsource : entitybase, new()
297         {
298             return db.deleteable<tsource>().in(ids).executecommand() > 0;
299         }
300 
301         #endregion
302 
303         #region 分页查询
304 
305         /// <summary>
306         /// 获取分页列表【页码,每页条数】
307         /// </summary>
308         /// <typeparam name="tsource">数据源类型</typeparam>
309         /// <param name="db"></param>
310         /// <param name="pageindex">页码(从0开始)</param>
311         /// <param name="pagesize">每页条数</param>
312         /// <returns></returns>
313         public static ipagedlist<tsource> getpagelist<tsource>(this sqlsugarclient db, int pageindex, int pagesize) where tsource : entitybase, new()
314         {
315             int count = 0;
316             var result = db.queryable<tsource>().topagelist(pageindex, pagesize, ref count);
317             return new pagedlist<tsource>(result, pageindex, pagesize, count);
318         }
319 
320         /// <summary>
321         /// 获取分页列表【页码,每页条数】
322         /// </summary>
323         /// <typeparam name="tsource">数据源类型</typeparam>
324         /// <typeparam name="tmap">数据源映射类型</typeparam>
325         /// <param name="db"></param>
326         /// <param name="pageindex">页码(从0开始)</param>
327         /// <param name="pagesize">每页条数</param>
328         /// <returns></returns>
329         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, int pageindex, int pagesize) where tsource : entitybase, new()
330         {
331             int count = 0;
332             var result = db.queryable<tsource>().topagelist(pageindex, pagesize, ref count);
333             var pageresult = new pagedlist<tsource>(result, pageindex, pagesize, count);
334             return pageresult.map<tsource, tmap>();
335         }
336 
337         #endregion
338 
339         #region 分页查询(排序)
340 
341         /// <summary>
342         /// 获取分页列表【排序,页码,每页条数】
343         /// </summary>
344         /// <typeparam name="tsource">数据源类型</typeparam>
345         /// <param name="db"></param>
346         /// <param name="orderexp">排序表达式</param>
347         /// <param name="ordertype">排序类型</param>
348         /// <param name="pageindex">页码(从0开始)</param>
349         /// <param name="pagesize">每页条数</param>
350         /// <returns></returns>
351         public static ipagedlist<tsource> getpagelist<tsource>(this sqlsugarclient db, expression<func<tsource, object>> orderexp, orderbytype ordertype, int pageindex, int pagesize) where tsource : entitybase, new()
352         {
353             int count = 0;
354             var result = db.queryable<tsource>().orderby(orderexp, ordertype).topagelist(pageindex, pagesize, ref count);
355             return new pagedlist<tsource>(result, pageindex, pagesize, count);
356         }
357 
358         /// <summary>
359         /// 获取分页列表【排序,页码,每页条数】
360         /// </summary>
361         /// <typeparam name="tsource">数据源类型</typeparam>
362         /// <typeparam name="tmap">数据源映射类型</typeparam>
363         /// <param name="db"></param>
364         /// <param name="orderexp">排序表达式</param>
365         /// <param name="ordertype">排序类型</param>
366         /// <param name="pageindex">页码(从0开始)</param>
367         /// <param name="pagesize">每页条数</param>
368         /// <returns></returns>
369         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, expression<func<tsource, object>> orderexp, orderbytype ordertype, int pageindex, int pagesize) where tsource : entitybase, new()
370         {
371             int count = 0;
372             var result = db.queryable<tsource>().orderby(orderexp, ordertype).topagelist(pageindex, pagesize, ref count);
373             var pageresult = new pagedlist<tsource>(result, pageindex, pagesize, count);
374             return pageresult.map<tsource, tmap>();
375         }
376 
377         #endregion
378 
379         #region 分页查询(linq表达式条件)
380 
381         /// <summary>
382         /// 获取分页列表【linq表达式条件,页码,每页条数】
383         /// </summary>
384         /// <typeparam name="tsource">数据源类型</typeparam>
385         /// <param name="db"></param>
386         /// <param name="whereexp">linq表达式条件</param>
387         /// <param name="pageindex">页码(从0开始)</param>
388         /// <param name="pagesize">每页条数</param>
389         /// <returns></returns>
390         public static ipagedlist<tsource> getpagelist<tsource>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp, int pageindex, int pagesize) where tsource : entitybase, new()
391         {
392             int count = 0;
393             var result = db.queryable<tsource>().where(whereexp).topagelist(pageindex, pagesize, ref count);
394             return new pagedlist<tsource>(result, pageindex, pagesize, count);
395         }
396 
397         /// <summary>
398         /// 获取分页列表【linq表达式条件,页码,每页条数】
399         /// </summary>
400         /// <typeparam name="tsource">数据源类型</typeparam>
401         /// <typeparam name="tmap">数据源映射类型</typeparam>
402         /// <param name="db"></param>
403         /// <param name="whereexp">linq表达式条件</param>
404         /// <param name="pageindex">页码(从0开始)</param>
405         /// <param name="pagesize">每页条数</param>
406         /// <returns></returns>
407         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp, int pageindex, int pagesize) where tsource : entitybase, new()
408         {
409             int count = 0;
410             var result = db.queryable<tsource>().where(whereexp).topagelist(pageindex, pagesize, ref count);
411             var pageresult = new pagedlist<tsource>(result, pageindex, pagesize, count);
412             return pageresult.map<tsource, tmap>();
413         }
414 
415         #endregion
416 
417         #region 分页查询(linq表达式条件,排序)
418 
419         /// <summary>
420         /// 获取分页列表【linq表达式条件,排序,页码,每页条数】
421         /// </summary>
422         /// <typeparam name="tsource">数据源类型</typeparam>
423         /// <param name="db"></param>
424         /// <param name="whereexp">linq表达式条件</param>
425         /// <param name="orderexp">排序表达式</param>
426         /// <param name="ordertype">排序类型</param>
427         /// <param name="pageindex">页码(从0开始)</param>
428         /// <param name="pagesize">每页条数</param>
429         /// <returns></returns>
430         public static ipagedlist<tsource> getpagelist<tsource>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp, expression<func<tsource, object>> orderexp, orderbytype ordertype, int pageindex, int pagesize) where tsource : entitybase, new()
431         {
432             int count = 0;
433             var result = db.queryable<tsource>().where(whereexp).orderby(orderexp, ordertype).topagelist(pageindex, pagesize, ref count);
434             return new pagedlist<tsource>(result, pageindex, pagesize, count);
435         }
436 
437         /// <summary>
438         ///  获取分页列表【linq表达式条件,排序,页码,每页条数】
439         /// </summary>
440         /// <typeparam name="tsource">数据源类型</typeparam>
441         /// <typeparam name="tmap">数据源映射类型</typeparam>
442         /// <param name="db"></param>
443         /// <param name="whereexp">linq表达式条件</param>
444         /// <param name="orderexp">排序表达式</param>
445         /// <param name="ordertype">排序类型</param>
446         /// <param name="pageindex">页码(从0开始)</param>
447         /// <param name="pagesize">每页条数</param>
448         /// <returns></returns>
449         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, expression<func<tsource, bool>> whereexp, expression<func<tsource, object>> orderexp, orderbytype ordertype, int pageindex, int pagesize) where tsource : entitybase, new()
450         {
451             int count = 0;
452             var result = db.queryable<tsource>().where(whereexp).orderby(orderexp, ordertype).topagelist(pageindex, pagesize, ref count);
453             var pageresult = new pagedlist<tsource>(result, pageindex, pagesize, count);
454             return pageresult.map<tsource, tmap>();
455         }
456 
457         #endregion
458 
459         #region 分页查询(sugar条件)
460 
461         /// <summary>
462         /// 获取分页列表【sugar表达式条件,页码,每页条数】
463         /// </summary>
464         /// <typeparam name="tsource">数据源类型</typeparam>
465         /// <param name="db"></param>
466         /// <param name="conditionals">sugar条件表达式集合</param>
467         /// <param name="pageindex">页码(从0开始)</param>
468         /// <param name="pagesize">每页条数</param>
469         /// <returns></returns>
470         public static ipagedlist<tsource> getpagelist<tsource>(this sqlsugarclient db, list<iconditionalmodel> conditionals, int pageindex, int pagesize) where tsource : entitybase, new()
471         {
472             int count = 0;
473             var result = db.queryable<tsource>().where(conditionals).topagelist(pageindex, pagesize, ref count);
474             return new pagedlist<tsource>(result, pageindex, pagesize, count);
475         }
476 
477         /// <summary>
478         /// 获取分页列表【sugar表达式条件,页码,每页条数】
479         /// </summary>
480         /// <typeparam name="tsource">数据源类型</typeparam>
481         /// <typeparam name="tmap">数据源映射类型</typeparam>
482         /// <param name="db"></param>
483         /// <param name="conditionals">sugar条件表达式集合</param>
484         /// <param name="pageindex">页码(从0开始)</param>
485         /// <param name="pagesize">每页条数</param>
486         /// <returns></returns>
487         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, list<iconditionalmodel> conditionals, int pageindex, int pagesize) where tsource : entitybase, new()
488         {
489             int count = 0;
490             var result = db.queryable<tsource>().where(conditionals).topagelist(pageindex, pagesize, ref count);
491             var pageresult = new pagedlist<tsource>(result, pageindex, pagesize, count);
492             return pageresult.map<tsource, tmap>();
493         }
494 
495         #endregion
496 
497         #region 分页查询(sugar条件,排序)
498 
499         /// <summary>
500         ///  获取分页列表【sugar表达式条件,排序,页码,每页条数】
501         /// </summary>
502         /// <typeparam name="tsource"></typeparam>
503         /// <param name="db"></param>
504         /// <param name="conditionals">sugar条件表达式集合</param>
505         /// <param name="orderexp">排序表达式</param>
506         /// <param name="ordertype">排序类型</param>
507         /// <param name="pageindex">页码(从0开始)</param>
508         /// <param name="pagesize">每页条数</param>
509         /// <returns></returns>
510         public static ipagedlist<tsource> getpagelist<tsource>(this sqlsugarclient db, list<iconditionalmodel> conditionals, expression<func<tsource, object>> orderexp, orderbytype ordertype, int pageindex, int pagesize) where tsource : entitybase, new()
511         {
512             int count = 0;
513             var result = db.queryable<tsource>().where(conditionals).orderby(orderexp, ordertype).topagelist(pageindex, pagesize, ref count);
514             return new pagedlist<tsource>(result, pageindex, pagesize, count);
515         }
516 
517         /// <summary>
518         ///  获取分页列表【sugar表达式条件,排序,页码,每页条数】
519         /// </summary>
520         /// <typeparam name="tsource"></typeparam>
521         /// <param name="db"></param>
522         /// <param name="conditionals">sugar条件表达式集合</param>
523         /// <param name="orderexp">排序表达式</param>
524         /// <param name="ordertype">排序类型</param>
525         /// <param name="pageindex">页码(从0开始)</param>
526         /// <param name="pagesize">每页条数</param>
527         /// <returns></returns>
528         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, list<iconditionalmodel> conditionals, expression<func<tsource, object>> orderexp, orderbytype ordertype, int pageindex, int pagesize) where tsource : entitybase, new()
529         {
530             int count = 0;
531             var result = db.queryable<tsource>().where(conditionals).orderby(orderexp, ordertype).topagelist(pageindex, pagesize, ref count);
532             var pageresult = new pagedlist<tsource>(result, pageindex, pagesize, count);
533             return pageresult.map<tsource, tmap>();
534         }
535 
536         #endregion
537 
538         #region 分页查询 (扩展条件构造实体,默认排序列,默认排序方式)
539         /// <summary>
540         /// 分页查询 (扩展条件构造实体,默认排序列,默认排序方式)
541         /// </summary>
542         /// <typeparam name="tsource"></typeparam>
543         /// <typeparam name="tmap"></typeparam>
544         /// <param name="db"></param>
545         /// <param name="query"></param>
546         /// <param name="defaultsort"></param>
547         /// <param name="defaultsorttype"></param>
548         /// <returns></returns>
549         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, querycollection query, expression<func<tsource, object>> defaultsort, orderbytype defaultsorttype) where tsource : entitybase, new()
550         {
551             int count = 0;
552             list<iconditionalmodel> conditionals = query.conditionitems.examineconditional<tsource>();
553             expression<func<tsource, object>> sort = query.sortlambda<tsource, object>(defaultsort, defaultsorttype, out var sorttype);
554             var result = db.queryable<tsource>().where(conditionals).orderby(sort, sorttype).topagelist(query.pageindex, query.pagesize, ref count);
555             var pageresult = new pagedlist<tsource>(result, query.pageindex, query.pagesize, count);
556             return pageresult.map<tsource, tmap>();
557         }
558         #endregion
559 
560         #region 分页查询 (扩展条件构造实体,默认排序列,默认排序方式,linq表达式条件)
561         /// <summary>
562         /// 分页查询 (扩展条件构造实体,默认排序列,默认排序方式,linq表达式条件)
563         /// </summary>
564         /// <typeparam name="tsource"></typeparam>
565         /// <typeparam name="tmap"></typeparam>
566         /// <param name="db"></param>
567         /// <param name="query"></param>
568         /// <param name="defaultsort"></param>
569         /// <param name="defaultsorttype"></param>
570         /// <param name="whereexp"></param>
571         /// <returns></returns>
572         public static ipagedlist<tmap> getpagelist<tsource, tmap>(this sqlsugarclient db, querycollection query, expression<func<tsource, object>> defaultsort, orderbytype defaultsorttype, expression<func<tsource, bool>> whereexp) where tsource : entitybase, new()
573         {
574             int count = 0;
575             list<iconditionalmodel> conditionals = query.conditionitems.examineconditional<tsource>();
576             expression<func<tsource, object>> sort = query.sortlambda<tsource, object>(defaultsort, defaultsorttype, out var sorttype);
577             var result = db.queryable<tsource>().where(whereexp).where(conditionals).orderby(sort, sorttype).topagelist(query.pageindex, query.pagesize, ref count);
578             var pageresult = new pagedlist<tsource>(result, query.pageindex, query.pagesize, count);
579             return pageresult.map<tsource, tmap>();
580         }
581         #endregion
582     }
sugarfactoryextensions,自行斟酌,喜欢使用sugar原生的,可跳过

使用:

net core 使用 SqlSugar
1             services.addsqlsugarclient<dbfactory>((sp, op) =>
2             {
3                 op.connectionstring = sp.getservice<iconfiguration>().getconnectionstring("lucy");
4                 op.dbtype = dbtype.mysql;
5                 op.isautocloseconnection = true;
6                 op.initkeytype = initkeytype.attribute;
7                 op.isshardsamethread = true;
8             });
注入
net core 使用 SqlSugar
 1     //如果数据操作简单,直接在业务层使用
 2     public class usersservice : repository<dbfactory, iusersrepository>, 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<users>(new users());
15             //第二种
16             using (var db = factory.getdbcontext())
17             {
18                 db.insert<users>(new users());
19                 db.update<users>(new users());
20             }
21             //数据操作繁琐的放到自定义的iusersrepository中
22             await dbrepository.testaddasync();
23         }
24 
25     }
业务层使用示例
net core 使用 SqlSugar
 1  public class usersrepository : repository<dbfactory>, 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<users>(new users());
13 
14             using (var db = factory.getdbcontext())
15             {
16                 db.insert<users>(new users());
17                 db.update<users>(new users());
18             }
19             return await task.fromresult(true);
20         }
21     }
仓储层(可省略,数据操作繁琐可以放这一层,简单的可以直接在业务层使用)