c# asp.net 身份和自定义角色
c# asp.net identity and custom roles
我想我只是在做一些傻事。
我首先使用代码 entity framework 和 asp.net 身份,我设置了一个自定义用户,如下所示:
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
class:
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>
我想我只是在做一些傻事。 我首先使用代码 entity framework 和 asp.net 身份,我设置了一个自定义用户,如下所示:
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
class:
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>