GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — 添加DbContext上下文可以注入获取 也可以手动获取,都是请求唯一, 添加DbContext扩展方法 · feier09/Emma.Blog@d5baefa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • Emma.Blog.Data
    • BlogContext.cs
    • DbContextExtensions.cs
    • DbInitizlaer.cs
    • Emma.Blog.Data.csproj
  • Emma.Blog.Service
    • Account
      • UserService.cs
    • Auth
      • ClaimUser.cs
    • DbContextFactory.cs
    • Emma.Blog.Service.csproj
    • MyHttpContext.cs
    • RegisterLogin
      • AccountService.cs
  • Emma.Blog.WebApi
    • Controllers
      • AuthController.cs
      • ValuesController.cs
    • Startup.cs
  • Emma.Blog.Web
    • Controllers
      • HomeController.cs
      • UserController.cs
    • Program.cs
    • Startup.cs
    • Views
      • Home
        • Index.cshtml
      • User
        • Info.cshtml
        • Login.cshtml
    • appsettings.json
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎Emma.Blog.Data/BlogContext.cs‎

Copy file name to clipboardExpand all lines: Emma.Blog.Data/BlogContext.cs
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@
44
using System.Text;
55
using Emma.Blog.Data.Models;
66
using Microsoft.EntityFrameworkCore.Extensions;
7-
7+
using System.Linq;
88
namespace Emma.Blog.Data
99
{
1010
public class BlogContext : DbContext
1111
{
12+
public Guid Id;
1213
public BlogContext(DbContextOptions<BlogContext> options) : base(options)
1314
{
14-
15+
Id = Guid.NewGuid();
1516
}
1617

1718

1819
public DbSet<User> Users { get; set; }
1920
public DbSet<Post> Posts { get; set; }
2021

21-
22+
2223

2324
}
2425
}
Collapse file

‎Emma.Blog.Data/DbContextExtensions.cs‎

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

‎Emma.Blog.Data/DbInitizlaer.cs‎

Copy file name to clipboardExpand all lines: Emma.Blog.Data/DbInitizlaer.cs
-34Lines changed: 0 additions & 34 deletions
This file was deleted.
Collapse file

‎Emma.Blog.Data/Emma.Blog.Data.csproj‎

Copy file name to clipboardExpand all lines: Emma.Blog.Data/Emma.Blog.Data.csproj
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@
1616
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
1717
</ItemGroup>
1818

19+
<ItemGroup>
20+
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
21+
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.0.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
22+
</Reference>
23+
</ItemGroup>
24+
1925
</Project>
Collapse file

‎Emma.Blog.Service/Account/UserService.cs‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Emma.Blog.Data;
3+
using Emma.Blog.Data.Models;
4+
using Emma.Blog.Service.Auth;
5+
using Microsoft.EntityFrameworkCore;
6+
7+
namespace Emma.Blog.Service.Account
8+
{
9+
public class UserService
10+
{
11+
protected DbContext dbContext = DbContextFactory.GetCurrentDbContext();
12+
13+
public IClaimUser<User> Login(string username,string password)
14+
{
15+
return new ClaimUser(new User());
16+
}
17+
18+
public IClaimUser<User> Register(User user)
19+
{
20+
21+
user.UserId = Guid.NewGuid();
22+
user.CreateTime = DateTime.Now;
23+
dbContext.Add(user);
24+
dbContext.SaveChanges();
25+
return new ClaimUser(user);
26+
}
27+
}
28+
}
Collapse file

‎Emma.Blog.Service/Auth/ClaimUser.cs‎

Copy file name to clipboardExpand all lines: Emma.Blog.Service/Auth/ClaimUser.cs
+7-7Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,32 @@
66

77
namespace Emma.Blog.Service.Auth
88
{
9-
public class ClaimUser : IClaimUser<Account>
9+
public class ClaimUser : IClaimUser<Data.Models.User>
1010
{
11-
private readonly Account _account;
12-
public ClaimUser(Account account)
11+
private readonly Data.Models.User _account;
12+
public ClaimUser(Data.Models.User account)
1313
{
1414
_account = account;
1515
}
1616

1717
public ClaimUser()
1818
{
19-
_account = new Account();
19+
_account = new Data.Models.User();
2020
}
2121
public List<Claim> GetClaims()
2222
{
2323
List<Claim> claims = new List<Claim>()
2424
{
25-
new Claim(ClaimTypes.Sid,_account.Id.ToString()),
25+
new Claim(ClaimTypes.Sid,_account.UserId.ToString()),
2626
new Claim(ClaimTypes.Name,_account.UserName),
27-
new Claim(ClaimTypes.Role, _account.AccountType.ToString()),
27+
2828

2929
};
3030
return claims;
3131
}
3232

3333

34-
public Account GetUser()
34+
public Data.Models.User GetUser()
3535
{
3636
return _account;
3737
}
Collapse file

‎Emma.Blog.Service/DbContextFactory.cs‎

Copy file name to clipboard
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Emma.Blog.Data;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.EntityFrameworkCore;
7+
8+
namespace Emma.Blog.Service
9+
{
10+
public class DbContextFactory
11+
{
12+
public static DbContext GetCurrentDbContext()
13+
{
14+
var httpContext = MyHttpContext.Current;
15+
var context = (DbContext)httpContext.RequestServices.GetService(typeof(BlogContext));
16+
17+
return context;
18+
}
19+
}
20+
}
Collapse file

‎Emma.Blog.Service/Emma.Blog.Service.csproj‎

Copy file name to clipboardExpand all lines: Emma.Blog.Service/Emma.Blog.Service.csproj
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12+
<Reference Include="Microsoft.AspNetCore.Http.Abstractions">
13+
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.aspnetcore.http.abstractions\2.0.0\lib\netstandard2.0\Microsoft.AspNetCore.Http.Abstractions.dll</HintPath>
14+
</Reference>
1215
<Reference Include="Microsoft.IdentityModel.Tokens">
1316
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.identitymodel.tokens\5.1.4\lib\netstandard1.4\Microsoft.IdentityModel.Tokens.dll</HintPath>
1417
</Reference>
@@ -17,4 +20,8 @@
1720
</Reference>
1821
</ItemGroup>
1922

23+
<ItemGroup>
24+
<Folder Include="RegisterLogin\" />
25+
</ItemGroup>
26+
2027
</Project>
Collapse file

‎Emma.Blog.Service/MyHttpContext.cs‎

Copy file name to clipboard
+29Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
6+
namespace Emma.Blog.Service
7+
{
8+
9+
public static class MyHttpContext
10+
{
11+
public static IServiceProvider ServiceProvider;
12+
13+
static MyHttpContext()
14+
{ }
15+
16+
17+
public static HttpContext Current
18+
{
19+
get
20+
{
21+
object factory = ServiceProvider.GetService(serviceType: typeof(Microsoft.AspNetCore.Http.IHttpContextAccessor));
22+
23+
HttpContext context = ((IHttpContextAccessor)factory).HttpContext;
24+
return context;
25+
}
26+
}
27+
}
28+
29+
}
Collapse file

‎Emma.Blog.Service/RegisterLogin/AccountService.cs‎

Copy file name to clipboardExpand all lines: Emma.Blog.Service/RegisterLogin/AccountService.cs
-29Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.