时间:2019-05-17 标签:c#asp.netidentityandcustomroles

标签 c# asp.net-core entity-framework-core asp.net-identity

我想我只是在这里做了一些愚蠢的事情。 我使用了具有 ASP.NET 身份的代码优先 Entity Framework ,并设置了一个如下所示的自定义用户:

public class User : IdentityUser, IKey<string>
{
    [MaxLength(100)] public string JobTitle { get; set; }
    [MaxLength(100)] public string Image { get; set; }
    [MaxLength(100)] public string FirstName { get; set; }
    [MaxLength(100)] public string LastName { get; set; }
}

然后我更新了我的DbContext匹配:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    // Customize the ASP.NET Identity model and override the defaults if needed.
    // For example, you can rename the ASP.NET Identity table names and more.
    // Add your customizations after calling base.OnModelCreating(builder);

    builder.Entity<User>(m => m.ToTable("Users"));
    builder.Entity<IdentityRole>(m => m.ToTable("Roles"));
    builder.Entity<IdentityRoleClaim<string>>(m => m.ToTable("RoleClaims"));
    builder.Entity<IdentityUserClaim<string>>(m => m.ToTable("UserClaims"));
    builder.Entity<IdentityUserLogin<string>>(m => m.ToTable("UserLogins"));
    builder.Entity<IdentityUserRole<string>>(m => m.ToTable("UserRoles"));
    builder.Entity<IdentityUserToken<string>>(m => m.ToTable("UserTokens"));
}

一切正常,表已成功创建。 现在我想对角色做同样的事情,只不过这次我不需要额外的列(这里重要的是界面),所以我创建了一个新的 Role类:

public class Role: IdentityRole, IKey<string>
{
}

然后我改变了我的OnModelCreating方法:

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
    // Customize the ASP.NET Identity model and override the defaults if needed.
    // For example, you can rename the ASP.NET Identity table names and more.
    // Add your customizations after calling base.OnModelCreating(builder);

    builder.Entity<User>(m => m.ToTable("Users"));
    builder.Entity<Role>(m => m.ToTable("Roles"));
    builder.Entity<IdentityRoleClaim<string>>(m => m.ToTable("RoleClaims"));
    builder.Entity<IdentityUserClaim<string>>(m => m.ToTable("UserClaims"));
    builder.Entity<IdentityUserLogin<string>>(m => m.ToTable("UserLogins"));
    builder.Entity<IdentityUserRole<string>>(m => m.ToTable("UserRoles"));
    builder.Entity<IdentityUserToken<string>>(m => m.ToTable("UserTokens"));
}

唯一改变的行是 builder.Entity<Role>(m => m.ToTable("Roles")); 。 当我运行add-migration RoleChange时我预计自上次迁移以来没有任何变化,但我收到了此错误:

The entity type 'Role' cannot be mapped to a table because it is derived from 'IdentityRole'. Only base entity types can be mapped to a table.

有人知道为什么吗? 我不明白为什么User有效,但是 Role不会....


以下是完整的上下文:

public class DatabaseContext : IdentityDbContext<User>
{
    public DbSet<Claim> Claims { get; set; }

    /// <summary>
    /// For testing only
    /// </summary>
    public DatabaseContext()
    {

    }

    // ReSharper disable once SuggestBaseTypeForParameter
    public DatabaseContext(DbContextOptions<DatabaseContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        builder.Entity<User>(m => m.ToTable("Users"));
        builder.Entity<Role>(m => m.ToTable("Roles"));
        builder.Entity<IdentityRoleClaim<string>>(m => m.ToTable("RoleClaims"));
        builder.Entity<IdentityUserClaim<string>>(m => m.ToTable("UserClaims"));
        builder.Entity<IdentityUserLogin<string>>(m => m.ToTable("UserLogins"));
        builder.Entity<IdentityUserRole<string>>(m => m.ToTable("UserRoles"));
        builder.Entity<IdentityUserToken<string>>(m => m.ToTable("UserTokens"));
    }
}

最佳答案

您所做的是正确的,您只需更新此行

public class DatabaseContext : IdentityDbContext<User>

如下:

public class DatabaseContext : IdentityDbContext<User, Role, string>

关于时间:2019-05-17 标签:c#asp.netidentityandcustomroles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64426229/

相关文章:

c# - 是否可以将表单控件放在自己的线程上?

c# - 为什么我的 ASP.NET Core 2 方法不能返回 TwiML?

c# - 加载X509证书: Error in Azure,无法在本地重现

c# - 无法在对象中插入重复的键 - 如何在 Entity Framework Core 中创建复合键

linq - Entity Framework 核心 GroupBy 日期

c# - 当没有其他对象引用它们时,Timer 对象是否会被 GC-ed?

c# - 订阅多个可观察对象的观察者未完成

c# - 如何在 aspnetcore 身份中显示未经授权的消息

c# - 使用授权 header 重定向到操作

c# - 是否可以从 EF Core 7 PropertyBuilder 获取提供程序信息