Microsoft Identity 自定义用户、角色、声明继承

Microsoft Identity Custom User, Role, Claim Inheritance

大家好,我正在尝试继承标准 AspNet tables。

我的意图是使用 Guid 作为主键并扩展基本的 AspNetUser 有一些属性。此外,我想重命名 table 个名称。

现在是这样的:

public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{

    public User() { }

    public User(string userName)
    {
        this.UserName = userName;
    }


    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
 }

我的 DbContext 看起来像这样:

 public class CustomDBContext : IdentityDbContext<User, Role, Guid, UserLogin, UserRole, UserClaim>
{

     public static CustomDBContext Create()
    {
        return new CustomDBContext();
    }

     protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityRole<Guid, UserRole>>().ToTable("Roles");
        modelBuilder.Entity<IdentityUserLogin<Guid>>().ToTable("UserLogins");
        modelBuilder.Entity<IdentityUserClaim<Guid>>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityUserRole<Guid>>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUser<Guid, UserLogin, UserRole, UserClaim>>().ToTable("Users");

    }
}

不幸的是,当我尝试生成我的迁移时。我收到那个错误:

The type 'Microsoft.AspNet.Identity.EntityFramework.IdentityRole`2[System.Guid,Easy1WebAPIData.Models.UserRole]' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive or generic, and does not inherit from EntityObject.

有什么想法吗?此致!

我相信您只需要在 OnModelCreating 方法中映射您的扩展模型。我正在对我的一个项目执行以下操作,它在迁移方面运行良好。

public class ApplicationUser : IdentityUser<Guid, CustomUserLogin, CustomUserRole, CustomUserClaim> { }
public class CustomUserRole : IdentityUserRole<Guid> { }
public class CustomUserClaim : IdentityUserClaim<Guid> { }
public class CustomUserLogin : IdentityUserLogin<Guid> { }
public class CustomRole : IdentityRole<Guid, CustomUserRole> { }

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, Guid, CustomUserLogin, CustomUserRole, CustomUserClaim> 
{
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
        modelBuilder.Entity<CustomRole>().ToTable("Roles");
        modelBuilder.Entity<CustomUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<CustomUserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<CustomUserClaim>().ToTable("UserClaims");
    }
}

此外,如果您使用 Guid 作为键,在对象的构造函数中设置 Id 是很常见的。

public class ApplicationUser : IdentityUser<Guid, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public ApplicationUser()
    {
        public ApplicationUser()
        {
            Id = Guid.NewGuid();
        }
    }
}

终于成功了!感谢@Phil Thomas

        modelBuilder.Entity<User>().ToTable("User").Property(p => p.Id).HasColumnName("UserID");
        modelBuilder.Entity<User>().Property(p => p.Email).HasColumnName("EmailAddress");

        modelBuilder.Entity<UserRole>().HasKey(r => new { r.RoleId, r.UserId });
        modelBuilder.Entity<UserRole>().ToTable("UserRole");
        modelBuilder.Entity<UserRole>().Property(r => r.UserId).HasColumnName("UserID");
        modelBuilder.Entity<UserRole>().Property(r => r.RoleId).HasColumnName("RoleID");

        modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<UserLogin>().Property(r => r.UserId).HasColumnName("UserID");

        modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<UserClaim>().Property(r => r.Id).HasColumnName("UserClaimID");

        modelBuilder.Entity<Role>().HasKey<Guid>(r => r.Id);
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<Role>().Property(r => r.Id).HasColumnName("RoleID");

        Configuration.ProxyCreationEnabled = false;
        Configuration.LazyLoadingEnabled = false;