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 : IdentityRole11 ????{12 ????????public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }13 ????????public virtual ICollection<ApplicationRoleClaim> RoleClaims { get; set; }14 ????}15 16 }
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.Models10 {11 12 ????public class ApplicationRoleClaim : IdentityRoleClaim<string>13 ????{14 ????????public virtual ApplicationRole Role { get; set; }15 ????}16 }
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 class10 ????public class ApplicationUserClaim : IdentityUserClaim<string>11 ????{12 ????????public virtual ApplicationUser User { get; set; }13 ????}14 }
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 class10 ????public class ApplicationUserLogin : IdentityUserLogin<string>11 ????{12 ????????public virtual ApplicationUser User { get; set; }13 ????}14 }
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 class10 ????public class ApplicationUserRole : IdentityUserRole<string>11 ????{12 ????????public virtual ApplicationUser User { get; set; }13 ????????public virtual ApplicationRole Role { get; set; }14 ????}15 }
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 class10 ????public class ApplicationUserToken : IdentityUserToken<string>11 ????{12 ????????public virtual ApplicationUser User { get; set; }13 ????}14 }
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.Data11 {12 ????public class ApplicationDbContext13 ????: 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 UserClaims30 ????????????????b.HasMany(e => e.Claims)31 ????????????????????.WithOne(e => e.User)32 ????????????????????.HasForeignKey(uc => uc.UserId)33 ????????????????????.IsRequired();34 35 ????????????????// Each User can have many UserLogins36 ????????????????b.HasMany(e => e.Logins)37 ????????????????????.WithOne(e => e.User)38 ????????????????????.HasForeignKey(ul => ul.UserId)39 ????????????????????.IsRequired();40 41 ????????????????// Each User can have many UserTokens42 ????????????????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 table48 ????????????????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 table58 ????????????????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 RoleClaims64 ????????????????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 }
包含关系建立,增加字段,修改自动生成在表名字3个功能,更详细的设置如长度,修改字段名字,可以通过连接参考
mvc core2.1 Identity.EntityFramework Core 实例配置 (四)
原文地址:https://www.cnblogs.com/LiuFengH/p/9419250.html