forked from chenzx1986/Emma.Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbContextExtensions.cs
More file actions
101 lines (85 loc) · 3.36 KB
/
DbContextExtensions.cs
File metadata and controls
101 lines (85 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace Microsoft.EntityFrameworkCore
{
public static class DbContextExtensions
{
public static IQueryable<T> PageAsQuery<T, TKey>(this DbContext dbContext,int pageIndex, int pageSize, out int totalCount, out int pageCount,
Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, TKey>> orderBy) where T : class, new()
{
IQueryable<T> temp = dbContext.Set<T>().Where(whereLambda).AsQueryable();
totalCount = temp.Count();
pageSize = Math.Min(pageSize, 30);
pageCount = (int)Math.Ceiling((double)totalCount / pageSize);
if (isAsc)
{
temp = temp.OrderBy(orderBy)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
}
else
{
temp = temp.OrderByDescending(orderBy)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
}
return temp.AsNoTracking();
}
public static IQueryable<T> PageAsQuery<T, TKey>(this DbContext dbContext,IQueryable<T> query, int pageIndex, int pageSize, out int totalCount, out int pageCount, bool isAsc, Expression<Func<T, TKey>> orderBy) where T : class, new()
{
IQueryable<T> temp = query;
totalCount = temp.Count();
pageSize = Math.Min(pageSize, 30);
pageCount = (int)Math.Ceiling((double)totalCount / pageSize);
if (isAsc)
{
temp = temp.OrderBy(orderBy)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
}
else
{
temp = temp.OrderByDescending(orderBy)
.Skip(pageSize * (pageIndex - 1))
.Take(pageSize).AsQueryable();
}
return temp;
}
public static bool Exist<T>(this DbContext dbContext,Expression<Func<T, bool>> anyLambda) where T : class, new()
{
return dbContext.Set<T>().Any(anyLambda);
}
public static T Find<T>(this DbContext dbContext,params object[] keyValues) where T : class, new()
{
return dbContext.Set<T>().Find(keyValues);
}
public static int Count<T>(this DbContext dbContext,Expression<Func<T, bool>> countLambda) where T : class, new()
{
return dbContext.Set<T>().Count(countLambda);
}
public static T First<T>(this DbContext dbContext,Expression<Func<T, bool>> firstLambda) where T : class, new()
{
return dbContext.Set<T>().FirstOrDefault(firstLambda);
}
public static int SaveChanges(this DbContext dbContext)
{
try
{
return dbContext.SaveChanges();
}
catch (DbUpdateException ex)
{
//Common.LogHelper.WriteLog(this.GetType(), ex.InnerException == null ? ex : ex.InnerException.InnerException);
throw;
}
catch (Exception ex)
{
//Common.LogHelper.WriteLog(this.GetType(), ex);
throw;
}
}
}
}