Mysql
 sql >> Cơ Sở Dữ Liệu >  >> RDS >> Mysql

EF Core 2.0 Identity - Thêm thuộc tính điều hướng

Tôi không biết tại sao, không có những thuộc tính điều hướng hữu ích này. Tôi muốn liệt kê những người dùng với vai trò của họ.

Vì vậy, tôi đã làm như sau:

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

Điều này tạo điều hướng, nhưng nó tạo thêm các cột như RoleId1Discriminator . Vì vậy, tôi đã thêm phần sau theo Thêm Thuộc tính Điều hướng POCO của Người dùng .

ghi đè
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);
}

Nhưng tôi vẫn có cả hai cột RoleId1Discriminator . Sau đó, tôi thay thế bằng lớp ApplicationRole mới trong ApplicationDbContext, dịch vụ cấu hình DI và hạt giống DB.

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

Ngoài ra, tôi có thể điều hướng và lấy tên đầu tiên của vai trò.

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

Tôi hy vọng điều này cung cấp cho bạn manh mối cho Claims thuộc tính điều hướng.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Đầu ra XML từ MySQL

  2. MySQL:Tôi có thể thực hiện phép nối bên trái và chỉ kéo một hàng từ bảng tham gia không?

  3. Chỉ cần chọn dữ liệu có chứa dấu gạch chéo ngược trong MySQL

  4. ClassNotFoundException:com.mysql.jdbc.GoogleDriver

  5. Cách có các giá trị thập phân chính xác mà không cần làm tròn trong MySQL