我想我只是在这里做了一些愚蠢的事情。 我使用了具有 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/