我认为我在这里做些愚蠢的事情。 我使用了具有asp身份的代码优先实体框架,并建立了这样的自定义用户:
公共类User: IdentityUser,IKey< string> { [MaxLength(100)]公共字符串JobTitle {get;组; } [MaxLength(100)]公共字符串Image {get;组; } [MaxLength(100)]公共字符串FirstName {get;组; } [MaxLength(100)]公共字符串LastName {get;组; } }然后我将 DbContext 更新为匹配:
受保护的覆盖无效OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); //自定义ASP.NET身份模型,并在需要时覆盖默认值。 //例如,您可以重命名ASP.NET标识表名称和更多名称。 //调用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 类:
公共类作用:IdentityRole,IKey< string> {}然后我更改了 OnModelCreating 方法:
受保护的覆盖无效OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder) ; //自定义ASP.NET身份模型,并在需要时覆盖默认值。 //例如,您可以重命名ASP.NET标识表名称和更多名称。 //调用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 时,我期望自上次迁移以来没有任何变化,但我却收到此错误:
实体类型'Role'不能映射到表,因为它是从'IdentityRole'派生的。只有基本实体类型可以映射到表。
有人知道为什么吗? 我不明白为什么 User 可以工作,但是 Role 不会。 p>
这里是完整上下文:
公共类DatabaseContext:IdentityDbContext< User> ; { public DbSet< Claim>要求{组; } ///< summary> ///仅用于测试 ///< / summary> public DatabaseContext() { } // ReSharper禁用一次ModifySimpleBaseTypeForParameter public DatabaseContext(DbContextOptions< DatabaseContext> options):base(options) {} 受保护的覆盖无效OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); //自定义ASP.NET身份模型,并在需要时覆盖默认值。 //例如,您可以重命名ASP.NET标识表名称和更多名称。 //调用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))); } }解决方案
您所做的操作是正确的,只需更新此行
公共类DatabaseContext:IdentityDbContext< User>如下:
公共类DatabaseContext:IdentityDbContext<用户,角色,字符串>
I think I am just doing something silly here. I have used code first entity framework with asp identity and I set up a custom user like this:
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; } }then I updated my DbContext to match:
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")); }All works, and the tables were created successfully. Now I want to do the same for roles, except this time I don't need extra columns (it's the interface that is important here) so I create a new Role class:
public class Role: IdentityRole, IKey<string> { }I then changed my OnModelCreating method to this:
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")); }The only line that changed was builder.Entity<Role>(m => m.ToTable("Roles"));. When I run add-migration RoleChange I expected nothing to have changed since my last migration, but instead I get this error:
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.
Does anyone know why? I don't understand why User works, but Role won't....
Here is the full context:
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")); } }解决方案
What you have done is correct, you just need to update this line
public class DatabaseContext : IdentityDbContext<User>as below :
public class DatabaseContext : IdentityDbContext<User, Role, string>
更多推荐
C#asp.net身份和自定义角色
发布评论