|
|
@@ -218,40 +218,72 @@ namespace Net6Demo_Api.Business
|
|
218
|
218
|
/// 更新一条数据
|
|
219
|
219
|
/// </summary>
|
|
220
|
220
|
/// <param name="entity">实体对象</param>
|
|
221
|
|
- public int Update(T entity)
|
|
|
221
|
+ /// <param name="properties">更新的实体属性</param>
|
|
|
222
|
+ public int Update(T entity, List<string> properties = null)
|
|
222
|
223
|
{
|
|
223
|
224
|
if (IsCache) OutRedis().Wait();
|
|
224
|
|
- return _db.Update(entity);
|
|
|
225
|
+ if (properties == null || properties.Count == 0)
|
|
|
226
|
+ {
|
|
|
227
|
+ return _db.Update(entity);
|
|
|
228
|
+ }
|
|
|
229
|
+ else
|
|
|
230
|
+ {
|
|
|
231
|
+ return _db.Update(entity, properties);
|
|
|
232
|
+ }
|
|
225
|
233
|
}
|
|
226
|
234
|
|
|
227
|
235
|
/// <summary>
|
|
228
|
236
|
/// 更新一条数据
|
|
229
|
237
|
/// </summary>
|
|
230
|
238
|
/// <param name="entity">实体对象</param>
|
|
231
|
|
- public async Task<int> UpdateAsync(T entity)
|
|
|
239
|
+ /// <param name="properties">更新的实体属性</param>
|
|
|
240
|
+ public async Task<int> UpdateAsync(T entity, List<string> properties = null)
|
|
232
|
241
|
{
|
|
233
|
242
|
if (IsCache) await OutRedis();
|
|
234
|
|
- return await _db.UpdateAsync(entity);
|
|
|
243
|
+ if (properties == null || properties.Count == 0)
|
|
|
244
|
+ {
|
|
|
245
|
+ return await _db.UpdateAsync(entity);
|
|
|
246
|
+ }
|
|
|
247
|
+ else
|
|
|
248
|
+ {
|
|
|
249
|
+ return await _db.UpdateAsync(entity, properties);
|
|
|
250
|
+ }
|
|
235
|
251
|
}
|
|
236
|
252
|
|
|
237
|
253
|
/// <summary>
|
|
238
|
254
|
/// 更新多条数据
|
|
239
|
255
|
/// </summary>
|
|
240
|
256
|
/// <param name="entities">数据列表</param>
|
|
241
|
|
- public int Update(List<T> entities)
|
|
|
257
|
+ /// <param name="properties">更新的实体属性</param>
|
|
|
258
|
+ public int Update(List<T> entities, List<string> properties = null)
|
|
242
|
259
|
{
|
|
243
|
260
|
if (IsCache) OutRedis().Wait();
|
|
244
|
|
- return _db.Update(entities);
|
|
|
261
|
+ if (properties == null || properties.Count == 0)
|
|
|
262
|
+ {
|
|
|
263
|
+ return _db.Update(entities);
|
|
|
264
|
+ }
|
|
|
265
|
+ else
|
|
|
266
|
+ {
|
|
|
267
|
+ return _db.Update(entities, properties);
|
|
|
268
|
+ }
|
|
245
|
269
|
}
|
|
246
|
270
|
|
|
247
|
271
|
/// <summary>
|
|
248
|
272
|
/// 更新多条数据
|
|
249
|
273
|
/// </summary>
|
|
250
|
274
|
/// <param name="entities">数据列表</param>
|
|
251
|
|
- public async Task<int> UpdateAsync(List<T> entities)
|
|
|
275
|
+ /// <param name="properties">更新的实体属性</param>
|
|
|
276
|
+ public async Task<int> UpdateAsync(List<T> entities, List<string> properties = null)
|
|
252
|
277
|
{
|
|
253
|
278
|
if (IsCache) await OutRedis();
|
|
254
|
|
- return await _db.UpdateAsync(entities);
|
|
|
279
|
+ if (properties == null || properties.Count == 0)
|
|
|
280
|
+ {
|
|
|
281
|
+ return await _db.UpdateAsync(entities);
|
|
|
282
|
+ }
|
|
|
283
|
+ else
|
|
|
284
|
+ {
|
|
|
285
|
+ return await _db.UpdateAsync(entities, properties);
|
|
|
286
|
+ }
|
|
255
|
287
|
}
|
|
256
|
288
|
|
|
257
|
289
|
/// <summary>
|
|
|
@@ -331,7 +363,7 @@ namespace Net6Demo_Api.Business
|
|
331
|
363
|
var listCache = GetRedis().Result;
|
|
332
|
364
|
if (listCache != null)
|
|
333
|
365
|
{
|
|
334
|
|
- return listCache.AsQueryable().Where(p=> KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
|
|
|
366
|
+ return listCache.AsQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
|
|
335
|
367
|
}
|
|
336
|
368
|
}
|
|
337
|
369
|
#endregion
|
|
|
@@ -348,7 +380,7 @@ namespace Net6Demo_Api.Business
|
|
348
|
380
|
#region 启用缓存
|
|
349
|
381
|
if (IsCache)
|
|
350
|
382
|
{
|
|
351
|
|
- var listCache =await GetRedis();
|
|
|
383
|
+ var listCache = await GetRedis();
|
|
352
|
384
|
if (listCache != null)
|
|
353
|
385
|
{
|
|
354
|
386
|
return listCache.AsQueryable().Where(p => KeyFiled.GetValue(p).ToString() == keyValue[0].ToString()).FirstOrDefault();
|
|
|
@@ -360,40 +392,149 @@ namespace Net6Demo_Api.Business
|
|
360
|
392
|
}
|
|
361
|
393
|
|
|
362
|
394
|
/// <summary>
|
|
363
|
|
- /// 分页获取数据
|
|
|
395
|
+ /// 根据条件获取数量
|
|
364
|
396
|
/// </summary>
|
|
365
|
|
- /// <param name="page">分页信息</param>
|
|
366
|
|
- /// <param name="orderByString">排序</param>
|
|
367
|
|
- /// <param name="condition">条件</param>
|
|
368
|
397
|
/// <returns></returns>
|
|
369
|
|
- public async Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, string orderByString, Expression<Func<T, bool>> condition)
|
|
|
398
|
+ public int GetCount(Expression<Func<T, bool>> condition)
|
|
370
|
399
|
{
|
|
371
|
|
- var iq = GetIQueryable().Where(condition);
|
|
372
|
|
- var list = await iq.OrderBy(orderByString).Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows).ToListAsync();
|
|
373
|
|
- int total = await iq.CountAsync();
|
|
|
400
|
+ #region 启用缓存
|
|
|
401
|
+ if (IsCache)
|
|
|
402
|
+ {
|
|
|
403
|
+ var listCache = GetRedis().Result;
|
|
|
404
|
+ if (listCache != null)
|
|
|
405
|
+ {
|
|
|
406
|
+ return listCache.AsQueryable().Where(condition).Count();
|
|
|
407
|
+ }
|
|
|
408
|
+ }
|
|
|
409
|
+ #endregion
|
|
374
|
410
|
|
|
375
|
|
- return (list, total);
|
|
|
411
|
+ return _db.GetIQueryable<T>().AsNoTracking().Where(condition).Count();
|
|
376
|
412
|
}
|
|
377
|
413
|
|
|
378
|
414
|
/// <summary>
|
|
379
|
|
- /// 获取实体对应的表,延迟加载,主要用于支持Linq查询操作
|
|
|
415
|
+ /// 根据条件获取数量
|
|
|
416
|
+ /// </summary>
|
|
|
417
|
+ /// <returns></returns>
|
|
|
418
|
+ public async Task<int> GetCountAsync(Expression<Func<T, bool>> condition)
|
|
|
419
|
+ {
|
|
|
420
|
+ #region 启用缓存
|
|
|
421
|
+ if (IsCache)
|
|
|
422
|
+ {
|
|
|
423
|
+ var listCache = await GetRedis();
|
|
|
424
|
+ if (listCache != null)
|
|
|
425
|
+ {
|
|
|
426
|
+ return listCache.AsQueryable().Where(condition).Count();
|
|
|
427
|
+ }
|
|
|
428
|
+ }
|
|
|
429
|
+ #endregion
|
|
|
430
|
+
|
|
|
431
|
+ return await _db.GetIQueryable<T>().AsNoTracking().Where(condition).CountAsync();
|
|
|
432
|
+ }
|
|
|
433
|
+
|
|
|
434
|
+ /// <summary>
|
|
|
435
|
+ /// 获取表的所有数据,当数据量很大时不要使用!
|
|
|
436
|
+ /// </summary>
|
|
|
437
|
+ /// <returns></returns>
|
|
|
438
|
+ public List<T> GetList()
|
|
|
439
|
+ {
|
|
|
440
|
+ #region 启用缓存
|
|
|
441
|
+ if (IsCache)
|
|
|
442
|
+ {
|
|
|
443
|
+ var listCache = GetRedis().Result;
|
|
|
444
|
+ if (listCache != null)
|
|
|
445
|
+ {
|
|
|
446
|
+ return listCache;
|
|
|
447
|
+ }
|
|
|
448
|
+ }
|
|
|
449
|
+ #endregion
|
|
|
450
|
+ return _db.GetIQueryable<T>().AsNoTracking().ToList();
|
|
|
451
|
+ }
|
|
|
452
|
+
|
|
|
453
|
+ /// <summary>
|
|
|
454
|
+ /// 根据条件获取列表,当数据量很大时不要使用!
|
|
380
|
455
|
/// </summary>
|
|
381
|
|
- /// <param name="IsNoCache">默认false,需要关联表查询时用true</param>
|
|
|
456
|
+ /// <param name="condition">条件</param>
|
|
382
|
457
|
/// <returns></returns>
|
|
383
|
|
- public IQueryable<T> GetIQueryable(bool IsNoCache = false)
|
|
|
458
|
+ public List<T> GetList(Expression<Func<T, bool>> condition)
|
|
384
|
459
|
{
|
|
385
|
460
|
#region 启用缓存
|
|
386
|
|
- if (IsCache && !IsNoCache)
|
|
|
461
|
+ if (IsCache)
|
|
387
|
462
|
{
|
|
388
|
463
|
var listCache = GetRedis().Result;
|
|
389
|
464
|
if (listCache != null)
|
|
390
|
465
|
{
|
|
391
|
|
- return listCache.AsQueryable();
|
|
|
466
|
+ return listCache.AsQueryable().Where(condition).ToList();
|
|
392
|
467
|
}
|
|
393
|
468
|
}
|
|
394
|
469
|
#endregion
|
|
395
|
470
|
|
|
396
|
|
- return _db.GetIQueryable<T>().AsNoTracking();
|
|
|
471
|
+ return _db.GetIQueryable<T>().AsNoTracking().Where(condition).ToList();
|
|
|
472
|
+ }
|
|
|
473
|
+
|
|
|
474
|
+ /// <summary>
|
|
|
475
|
+ /// 获取表的所有数据,当数据量很大时不要使用!
|
|
|
476
|
+ /// </summary>
|
|
|
477
|
+ /// <returns></returns>
|
|
|
478
|
+ public async Task<List<T>> GetListAsync()
|
|
|
479
|
+ {
|
|
|
480
|
+ #region 启用缓存
|
|
|
481
|
+ if (IsCache)
|
|
|
482
|
+ {
|
|
|
483
|
+ var listCache = await GetRedis();
|
|
|
484
|
+ if (listCache != null)
|
|
|
485
|
+ {
|
|
|
486
|
+ return listCache;
|
|
|
487
|
+ }
|
|
|
488
|
+ }
|
|
|
489
|
+ #endregion
|
|
|
490
|
+ return await _db.GetIQueryable<T>().AsNoTracking().ToListAsync();
|
|
|
491
|
+ }
|
|
|
492
|
+
|
|
|
493
|
+ /// <summary>
|
|
|
494
|
+ /// 根据条件获取列表,当数据量很大时不要使用!
|
|
|
495
|
+ /// </summary>
|
|
|
496
|
+ /// <param name="condition">条件</param>
|
|
|
497
|
+ /// <returns></returns>
|
|
|
498
|
+ public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> condition)
|
|
|
499
|
+ {
|
|
|
500
|
+ #region 启用缓存
|
|
|
501
|
+ if (IsCache)
|
|
|
502
|
+ {
|
|
|
503
|
+ var listCache = await GetRedis();
|
|
|
504
|
+ if (listCache != null)
|
|
|
505
|
+ {
|
|
|
506
|
+ return listCache.AsQueryable().Where(condition).ToList();
|
|
|
507
|
+ }
|
|
|
508
|
+ }
|
|
|
509
|
+ #endregion
|
|
|
510
|
+
|
|
|
511
|
+ return await _db.GetIQueryable<T>().AsNoTracking().Where(condition).ToListAsync();
|
|
|
512
|
+ }
|
|
|
513
|
+
|
|
|
514
|
+ /// <summary>
|
|
|
515
|
+ /// 分页获取数据
|
|
|
516
|
+ /// </summary>
|
|
|
517
|
+ /// <param name="page">分页信息</param>
|
|
|
518
|
+ /// <param name="orderByString">排序</param>
|
|
|
519
|
+ /// <param name="condition">条件</param>
|
|
|
520
|
+ /// <returns></returns>
|
|
|
521
|
+ public async Task<ValueTuple<List<T>, int>> GetListPageAsync(PageInput page, string orderByString, Expression<Func<T, bool>> condition)
|
|
|
522
|
+ {
|
|
|
523
|
+ var iq = _db.GetIQueryable<T>().AsNoTracking().Where(condition).OrderBy(orderByString);
|
|
|
524
|
+
|
|
|
525
|
+ var list = await iq.Skip((page.PageIndex - 1) * page.PageRows).Take(page.PageRows).ToListAsync();
|
|
|
526
|
+ int total = await iq.CountAsync();
|
|
|
527
|
+
|
|
|
528
|
+ return (list, total);
|
|
|
529
|
+ }
|
|
|
530
|
+
|
|
|
531
|
+ /// <summary>
|
|
|
532
|
+ /// 获取实体对应的表,延迟加载,主要用于支持Linq查询操作
|
|
|
533
|
+ /// </summary>
|
|
|
534
|
+ /// <returns></returns>
|
|
|
535
|
+ public virtual IQueryable<T> GetIQueryable()
|
|
|
536
|
+ {
|
|
|
537
|
+ return _db.GetIQueryable<T>();
|
|
397
|
538
|
}
|
|
398
|
539
|
|
|
399
|
540
|
#endregion
|