Microsoft身份自定义用户,角色,索赔继承



嗨,我我试图从标准ASPNET表继承。

我的目的是将GUID用作主要钥匙并扩展基本的Aspnetuser具有一些属性。此外,我想重命名表名。

现在就像:

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

不幸的是,当IAM试图产生我的迁移时。我得到了这个错误:

'Microsoft.aspnet.Identity.EntityFramework.IdentityRole`2 [System.Guid,easy1webapidata.models.userrole]''未映射。检查该类型尚未通过使用忽略方法或未映射的数据注释明确排除该类型。验证该类型被定义为类,不是原始的或通用的,并且不会从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();
        }
    }
}

终于起作用了!THX到@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;

相关内容

  • 没有找到相关文章

最新更新