EF Core 2.0身份-添加导航属性 [英] EF Core 2.0 Identity - Adding navigation properties

查看:106
本文介绍了EF Core 2.0身份-添加导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EF Core 2.0中,默认情况下不包含导航属性,因此在升级后,我添加了它们.因此,对于用户和角色之间的多对多关系以及角色和RoleClaim之间的一对多关系,我添加了以下导航属性:

In EF Core 2.0 Identity navigation properties are not included by default, so after upgrading, I added them. So for Many-To-Many relationship between User and Role, and One-To-Many relationship between Role and RoleClaim, I added following navigation properties:

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;}
}

令人惊讶的是,它向AspNetRoleClaims表添加了RoleId1键,向AspNetUserRoles表添加了UserId1键,并且所有的get查询实际上都使用了新的键,而不是同时存在的RoleIdUserId. /p>

Surprisingly it adds an additional RoleId1 key to AspNetRoleClaims table and UserId1 to AspNetUserRoles tables and all the get queries actually use new keys instead of RoleId and UserId which are also present.

推荐答案

我不知道为什么,没有这些有用的导航属性.我想列出用户及其角色.

I do not know why, there are not these useful navigation properties. I want to list users with their roles.

所以我做了以下事情:

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>();
}

这将创建导航,但会创建其他列,例如RoleId1Discriminator.因此,我根据

This creates the navigation, but it creates additional columns like RoleId1 and Discriminator. So, I added the following according to 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);
}

但是我仍然同时拥有RoleId1Discriminator列.之后,我在ApplicationDbContext,DI配置服务和数据库种子中替换为新的ApplicationRole类.

But I still have both columns RoleId1 and Discriminator. After that, I replace with the new ApplicationRole class in ApplicationDbContext, DI configuration service and DB seed.

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));
    }            

此外,我可以导航并获取角色的名字.

Also, I could navigate and get the first name of role.

ctx.Users.Select(e => new
            {
                e.Id,
                e.UserName,
                e.Email,
                e.PhoneNumber,
                Roles = e.UserRoles.Select(i => i.Role.Name).ToList()
            }).ToList();

我希望这会为您提供Claims导航属性的线索.

I hope this give you a clue for Claims navigation property.

这篇关于EF Core 2.0身份-添加导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆