adn*_*ili 7 c# mysql postgresql entity-framework-core
在 EF Core 2.0 中,默认情况下不包含身份导航属性,因此在升级后,我添加了它们。因此,对于 User 和 Role 之间的多对多关系,以及 Role 和 RoleClaim 之间的一对多关系,我添加了以下导航属性:
public class User : IdentityUser
{
[Required]
public string Name { get; set; }
public virtual ICollection<IdentityUserRole<string>> Roles { get; set; }
}
public class Role : IdentityRole
{
[Required]
public string Name { get; set; }
public virtual ICollection<IdentityRoleClaim<string>> Claims { get; set;}
}
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是增加了一个额外RoleId1关键AspNetRoleClaims表和UserId1对AspNetUserRoles表和所有的GET查询实际使用新的密钥,而不是RoleId和UserId它也存在。
UUH*_*IVS 10
不知道为什么,没有这些有用的导航属性。我想列出用户及其角色。
所以我做了以下事情:
public class ApplicationUser : IdentityUser
{
public virtual ICollection<ApplicationUserRole> UserRoles { get; } = new List<ApplicationUserRole>();
}
public class ApplicationUserRole : IdentityUserRole<string>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationRole : IdentityRole<string>
{
public ApplicationRole(){ }
public ApplicationRole(string roleName)
: base(roleName)
{
}
public virtual ICollection<ApplicationUserRole> UserRoles { get; } = new List<ApplicationUserRole>();
}
Run Code Online (Sandbox Code Playgroud)
这会创建导航,但会创建额外的列,例如RoleId1和Discriminator。所以,我根据Add IdentityUser POCO Navigation Properties添加了以下内容。
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>()
.HasMany(e => e.UserRoles)
.WithOne()
.HasForeignKey(e => e.UserId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<ApplicationUserRole>()
.HasOne(e => e.User)
.WithMany(e => e.UserRoles)
.HasForeignKey(e => e.UserId);
builder.Entity<ApplicationUserRole>()
.HasOne(e => e.Role)
.WithMany(e => e.UserRoles)
.HasForeignKey(e => e.RoleId);
}
Run Code Online (Sandbox Code Playgroud)
但我仍然有列RoleId1和Discriminator. 之后,我用 ApplicationDbContext、DI 配置服务和 DB 种子中的新 ApplicationRole 类替换。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserClaim<string>
, ApplicationUserRole, IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>>
{
...
}
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
...
}
public DbInitializer(
ApplicationDbContext context,
UserManager<ApplicationUser> userManager,
RoleManager<ApplicationRole> roleManager)
{
_context = context;
_userManager = userManager;
_roleManager = roleManager;
}
public async void Initialize()
{
_context.Database.EnsureCreated();
if (!_context.Roles.Any(r => r.Name == SharedConstants.Role.ADMINISTRATOR))
await _roleManager.CreateAsync(new ApplicationRole(SharedConstants.Role.ADMINISTRATOR));
}
Run Code Online (Sandbox Code Playgroud)
此外,我可以导航并获取角色的名字。
ctx.Users.Select(e => new
{
e.Id,
e.UserName,
e.Email,
e.PhoneNumber,
Roles = e.UserRoles.Select(i => i.Role.Name).ToList()
}).ToList();
Run Code Online (Sandbox Code Playgroud)
我希望这可以为您提供Claims导航属性的线索。
| 归档时间: |
|
| 查看次数: |
3471 次 |
| 最近记录: |