11using Microsoft . AspNetCore . Identity . EntityFrameworkCore ;
22using Microsoft . EntityFrameworkCore ;
3- using System ;
4- using System . Collections . Generic ;
5- using System . Text ;
3+ using SampleMvcWebWithUi . Models ;
64
75namespace 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