Skip to content

Commit 84d6672

Browse files
committed
Update .NET 5 sample project to IdentityDbContext with 8 generic parameters.
1 parent 6dbd2ab commit 84d6672

16 files changed

+422
-232
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
1-
using Microsoft.AspNetCore.Mvc;
1+
using Microsoft.AspNetCore.Identity;
2+
using Microsoft.AspNetCore.Mvc;
23
using Microsoft.Extensions.Logging;
34
using SampleMvcWebWithUi.Models;
4-
using System;
5-
using System.Collections.Generic;
65
using System.Diagnostics;
7-
using System.Linq;
86
using System.Threading.Tasks;
97

108
namespace SampleMvcWebWithUi.Controllers
119
{
1210
public class HomeController : Controller
1311
{
1412
private readonly ILogger<HomeController> _logger;
13+
private readonly RoleManager<ApplicationRole> _roleManager;
1514

16-
public HomeController(ILogger<HomeController> logger)
15+
public HomeController(ILogger<HomeController> logger, RoleManager<ApplicationRole> roleManager)
1716
{
1817
_logger = logger;
18+
_roleManager = roleManager;
1919
}
2020

21-
public IActionResult Index()
21+
public async Task<IActionResult> Index()
2222
{
23+
//var role = new ApplicationRole { Name = "Test1" };
24+
//var result = await _roleManager.CreateAsync(role);
25+
2326
return View();
2427
}
2528

@@ -34,4 +37,4 @@ public IActionResult Error()
3437
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
3538
}
3639
}
37-
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,62 @@
11
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
22
using Microsoft.EntityFrameworkCore;
3-
using System;
4-
using System.Collections.Generic;
5-
using System.Text;
3+
using SampleMvcWebWithUi.Models;
64

75
namespace SampleMvcWebWithUi.Data
86
{
9-
public class ApplicationDbContext : IdentityDbContext
7+
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int,
8+
ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin, ApplicationRoleClaim, ApplicationUserToken>
109
{
1110
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
1211
: base(options)
1312
{
1413
}
14+
15+
protected override void OnModelCreating(ModelBuilder modelBuilder)
16+
{
17+
base.OnModelCreating(modelBuilder);
18+
19+
modelBuilder.Entity<ApplicationUser>(b =>
20+
{
21+
// Each User can have many UserClaims
22+
b.HasMany(e => e.Claims)
23+
.WithOne(e => e.User)
24+
.HasForeignKey(uc => uc.UserId)
25+
.IsRequired();
26+
27+
// Each User can have many UserLogins
28+
b.HasMany(e => e.Logins)
29+
.WithOne(e => e.User)
30+
.HasForeignKey(ul => ul.UserId)
31+
.IsRequired();
32+
33+
// Each User can have many UserTokens
34+
b.HasMany(e => e.Tokens)
35+
.WithOne(e => e.User)
36+
.HasForeignKey(ut => ut.UserId)
37+
.IsRequired();
38+
39+
// Each User can have many entries in the UserRole join table
40+
b.HasMany(e => e.UserRoles)
41+
.WithOne(e => e.User)
42+
.HasForeignKey(ur => ur.UserId)
43+
.IsRequired();
44+
});
45+
46+
modelBuilder.Entity<ApplicationRole>(b =>
47+
{
48+
// Each Role can have many entries in the UserRole join table
49+
b.HasMany(e => e.UserRoles)
50+
.WithOne(e => e.Role)
51+
.HasForeignKey(ur => ur.RoleId)
52+
.IsRequired();
53+
54+
// Each Role can have many associated RoleClaims
55+
b.HasMany(e => e.RoleClaims)
56+
.WithOne(e => e.Role)
57+
.HasForeignKey(rc => rc.RoleId)
58+
.IsRequired();
59+
});
60+
}
1561
}
16-
}
62+
}

0 commit comments

Comments
 (0)