码迷,mamicode.com
首页 > Web开发 > 详细

mvc core2.1 Identity.EntityFramework Core 实例配置 (四)

时间:2018-08-04 18:45:24      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:virtual   OLE   over   build   creating   work   zh-cn   生成   hide   

  https://docs.microsoft.com/zh-cn/aspnet/core/security/authentication/customize_identity_model?view=aspnetcore-2.1 实践

Models->ApplicationRole.cs

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore.Identity;
 7 
 8 namespace IdentityMvc.Models
 9 {
10     public class ApplicationRole : IdentityRole
11     {
12         public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
13         public virtual ICollection<ApplicationRoleClaim> RoleClaims { get; set; }
14     }
15 
16 }
View Code

Models->ApplicationRoleClaim.cs

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel.DataAnnotations;
 4 using System.Linq;
 5 using System.Threading.Tasks;
 6 using Microsoft.AspNetCore.Identity;
 7 using IdentityMvc.Models;
 8 
 9 namespace IdentityMvc.Models
10 {
11 
12     public class ApplicationRoleClaim : IdentityRoleClaim<string>
13     {
14         public virtual ApplicationRole Role { get; set; }
15     }
16 }
View Code

Models-> ApplicationUser 添加

        public string Note {get;set;} //自定义添加字段

        public virtual ICollection<ApplicationUserClaim> Claims { get; set; }
        public virtual ICollection<ApplicationUserLogin> Logins { get; set; }
        public virtual ICollection<ApplicationUserToken> Tokens { get; set; }
        public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }

Models-> ApplicationUserClaim.cs 新建

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity;
 6 
 7 namespace IdentityMvc.Models
 8 {
 9     // Add profile data for application users by adding properties to the ApplicationUser class
10     public class ApplicationUserClaim : IdentityUserClaim<string>
11     {
12         public virtual ApplicationUser User { get; set; }
13     }
14 }
View Code

Models->ApplicationUserLogin.cs

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity;
 6 
 7 namespace IdentityMvc.Models
 8 {
 9     // Add profile data for application users by adding properties to the ApplicationUser class
10     public class ApplicationUserLogin : IdentityUserLogin<string>
11     {
12         public virtual ApplicationUser User { get; set; }
13     }
14 }
View Code

Models->ApplicationUserRole.cs

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity;
 6 
 7 namespace IdentityMvc.Models
 8 {
 9     // Add profile data for application users by adding properties to the ApplicationUser class
10     public class ApplicationUserRole : IdentityUserRole<string>
11     {
12         public virtual ApplicationUser User { get; set; }
13         public virtual ApplicationRole Role { get; set; }
14     }
15 }
View Code

Models->ApplicationUserToken.cs

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity;
 6 
 7 namespace IdentityMvc.Models
 8 {
 9     // Add profile data for application users by adding properties to the ApplicationUser class
10     public class ApplicationUserToken : IdentityUserToken<string>
11     {
12         public virtual ApplicationUser User { get; set; }
13     }
14 }
View Code

Data->ApplicationDbContext.cs修改

技术分享图片
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Threading.Tasks;
 5 using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
 6 using Microsoft.EntityFrameworkCore;
 7 using IdentityMvc.Models;
 8 using Microsoft.AspNetCore.Identity;
 9 
10 namespace IdentityMvc.Data
11 {
12     public class ApplicationDbContext
13     : IdentityDbContext<
14         ApplicationUser, ApplicationRole, string,
15         ApplicationUserClaim, ApplicationUserRole, ApplicationUserLogin,
16         ApplicationRoleClaim, ApplicationUserToken>
17 {
18     public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
19         : base(options)
20     {
21     }
22     
23     protected override void OnModelCreating(ModelBuilder modelBuilder)
24     {
25         base.OnModelCreating(modelBuilder);
26 
27             modelBuilder.Entity<ApplicationUser>(b =>
28             {
29                 // Each User can have many UserClaims
30                 b.HasMany(e => e.Claims)
31                     .WithOne(e => e.User)
32                     .HasForeignKey(uc => uc.UserId)
33                     .IsRequired();
34 
35                 // Each User can have many UserLogins
36                 b.HasMany(e => e.Logins)
37                     .WithOne(e => e.User)
38                     .HasForeignKey(ul => ul.UserId)
39                     .IsRequired();
40 
41                 // Each User can have many UserTokens
42                 b.HasMany(e => e.Tokens)
43                     .WithOne(e => e.User)
44                     .HasForeignKey(ut => ut.UserId)
45                     .IsRequired();
46 
47                 // Each User can have many entries in the UserRole join table
48                 b.HasMany(e => e.UserRoles)
49                     .WithOne(e => e.User)
50                     .HasForeignKey(ur => ur.UserId)
51                     .IsRequired();
52                 b.ToTable("Sys_Users");
53             });
54 
55             modelBuilder.Entity<ApplicationRole>(b =>
56             {
57                 // Each Role can have many entries in the UserRole join table
58                 b.HasMany(e => e.UserRoles)
59                     .WithOne(e => e.Role)
60                     .HasForeignKey(ur => ur.RoleId)
61                     .IsRequired();
62 
63                 // Each Role can have many associated RoleClaims
64                 b.HasMany(e => e.RoleClaims)
65                     .WithOne(e => e.Role)
66                     .HasForeignKey(rc => rc.RoleId)
67                     .IsRequired();
68                 b.ToTable("Sys_Roles");
69             });
70             modelBuilder.Entity<ApplicationUserClaim>(b =>
71             {
72                 b.ToTable("Sys_UserClaims");
73             });
74 
75             modelBuilder.Entity<ApplicationUserLogin>(b =>
76             {
77                 b.ToTable("Sys_UserLogins");
78             });
79 
80             modelBuilder.Entity<ApplicationUserToken>(b =>
81             {
82                 b.ToTable("Sys_UserTokens");
83             });
84 
85             modelBuilder.Entity<ApplicationRoleClaim>(b =>
86             {
87                 b.ToTable("Sys_RoleClaims");
88             });
89 
90             modelBuilder.Entity<ApplicationUserRole>(b =>
91             {
92                 b.ToTable("Sys_UserRoles");
93             });
94         }
95     }
96 }
View Code

包含关系建立,增加字段,修改自动生成在表名字3个功能,更详细的设置如长度,修改字段名字,可以通过连接参考

 

mvc core2.1 Identity.EntityFramework Core 实例配置 (四)

标签:virtual   OLE   over   build   creating   work   zh-cn   生成   hide   

原文地址:https://www.cnblogs.com/LiuFengH/p/9419250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!