EF Core 2.0 Identity - 添加导航属性

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表和UserId1AspNetUserRoles表和所有的GET查询实际使用新的密钥,而不是RoleIdUserId它也存在。

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)

这会创建导航,但会创建额外的列,例如RoleId1Discriminator。所以,我根据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)

但我仍然有列RoleId1Discriminator. 之后,我用 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导航属性的线索。


iPa*_*h ツ 5

我遇到同样的问题,在我的情况的问题造成的,因为我已经把电话给base.OnModelCreating(builder)在底部OnModelCreatingbase.OnModelCreating(builder)在最顶端移动解决了这个问题(没有重复的列和 FK)。

在此处输入图片说明

感谢这个GitHub 问题

  • 这为我解决了这个问题。@UUHHIVS 对于“设置”有一个很好的答案。不过,上面的答案解决了我的问题^^ (2认同)