我在带有.NET Framework的ASP.NET MVC 5中有一个项目,我正在使用Identity来管理用户授权。项目的要求不允许我在数据库中留下默认的ASPNET-(某物(名称。另外,我需要在角色和用户中添加一些自定义属性。因此,我创建了一个自定义用户和角色类,例如:
public class ApplicationRole : IdentityRole
{
public virtual ICollection<RoleApllicationModel> RolesApplications { get; set; }
public string description{get; set;}
}
如果我更改此类名称的名称,则使用此类:
modelBuilder.Entity<ApplicationUser>().ToTable("SSO_USERS");
modelBuilder.Entity<ApplicationRole>().ToTable("SSO_ROLES");
modelBuilder.Entity<IdentityUserLogin>().ToTable("SSO_USER_LOGINS");
modelBuilder.Entity<IdentityUserRole>().ToTable("SSO_USERS_ROLES");
modelBuilder.Entity<IdentityUserClaim>().ToTable("SSO_USER_CLAIMS");
当它创建数据库时,它会添加第二个角色,其中一个名为SSO_ROLES
和一个AspNetRoles
。aspnetroles
表具有id
和name
,第二个表具有description
,并且指向aspnetroles
表的外键。
这是没有意义的,我想知道是否有一种方法可以避免这种行为发生,并且仅使用id
,name
和description
创建表SSO_ROLES
。如果我删除描述属性,则仅创建SSO_ROLES
表。
编辑:
这是身份的上下文
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<ApplicationModel> Applications { get; set; }
public DbSet<TokenModel> Tokens { get; set; }
public DbSet<UserApllicationModel> UsersApplications { get; set; }
public DbSet<RoleApllicationModel> RolesApplications { get; set; }
static ApplicationDbContext()
{
// Set the database intializer which is run once during application start
// This seeds the database with admin user credentials and admin role
Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("SSO_USUARIOS");
modelBuilder.Entity<ApplicationRole>().ToTable("SSO_ROLE");
modelBuilder.Entity<IdentityUserLogin>().ToTable("SSO_USUARIO_LOGINS");
modelBuilder.Entity<IdentityUserRole>().ToTable("SSO_USUARIOS_ROLES");
modelBuilder.Entity<IdentityUserClaim>().ToTable("SSO_USUARIO_CLAIMS");
}
}
我找到了解决方案,如果有人处理相同的问题。您还需要将默认身份类别添加到ModelBuilder中,例如:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("SSO_USERS");
modelBuilder.Entity<IdentityUser>().ToTable("SSO_USERS");
modelBuilder.Entity<ApplicationRole>().ToTable("SSO_ROLES");
modelBuilder.Entity<IdentityRole>().ToTable("SSO_ROLES");
modelBuilder.Entity<IdentityUserLogin>().ToTable("SSO_USER_LOGINS");
modelBuilder.Entity<IdentityUserRole>().ToTable("SSO_USERS_ROLES");
modelBuilder.Entity<IdentityUserClaim>().ToTable("SSO_USER_CLAIMS");
}
如果您使用的是诸如ApplicationRole之类的自定义类,请添加带有同一表名称的原始身份。