1
1
using Microsoft . AspNetCore . Identity . EntityFrameworkCore ;
2
2
using Microsoft . EntityFrameworkCore ;
3
- using System ;
4
- using System . Collections . Generic ;
5
- using System . Text ;
3
+ using SampleMvcWebWithUi . Models ;
6
4
7
5
namespace SampleMvcWebWithUi . Data
8
6
{
9
- public class ApplicationDbContext : IdentityDbContext
7
+ public class ApplicationDbContext : IdentityDbContext < ApplicationUser , ApplicationRole , int ,
8
+ ApplicationUserClaim , ApplicationUserRole , ApplicationUserLogin , ApplicationRoleClaim , ApplicationUserToken >
10
9
{
11
10
public ApplicationDbContext ( DbContextOptions < ApplicationDbContext > options )
12
11
: base ( options )
13
12
{
14
13
}
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
+ }
15
61
}
16
- }
62
+ }
0 commit comments