2017-08-24 9 views
1

EF Core 2.0のアイデンティティナビゲーションのプロパティはデフォルトでは含まれていないため、アップグレード後に追加しました。だから、ユーザーおよびロール間の多対多の関係のために、そして役割とRoleClaimの間には、1対多の関係、私は、ナビゲーションプロパティを、次の追加:EF Core 2.0のアイデンティティ - ナビゲーションプロパティの追加

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キーを追加し、UserId1AspNetUserRolesテーブルにあり、すべてのgetクエリは実際にはRoleIdUserIdの代わりに新しいキーを使用します。

+0

このスレッドの回答を見るhttps://stackoverflow.com/a/47772406/82197 – leen3o

答えて

2

理由はわかりませんが、これらの便利なナビゲーションプロパティはありません。自分の役割を持つユーザーをリストしたいと思います。

だから私は、フォローでした:

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などの追加の列を作成します。そこで、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構成サービス、および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)); 
    }    

また、私はナビゲートしてロールのファーストネームを得ることができます。

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

これはあなたにClaimsナビゲーションプロパティの手がかりを与えることを望みます。

+0

非常に徹底的な答えをいただきありがとうございます。私はちょうどこれに逆らって、あなたの助言に従いました、そして、今行くのは良いです! –

関連する問題