自定义 asp.net 身份角色正在迁移脚本中创建额外的 table
Customizing asp.net identityrole is creating additional table in migration script
我正在尝试自定义 identityRole table 以添加新属性,但它正在创建新的 table 脚本而不是在脚本中创建属性。如何生成使用 db.tb_appRoles table 而不是 db.aspnetRoles 和 db.tb_appRoles 的脚本?
我的配置如下:
public class AppRole : IdentityRole
{
public string Description { get; set; }
}
public class AppRoleMapping : EntityTypeConfiguration<AppRole>
{
public AppRoleMapping()
: base()
{
Map(m =>
{
m.ToTable("tb_AppRole");
}).HasKey(e => e.Id); ;
}
}
// Generated script with two separate tables
public partial class initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.tb_AppRole",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Description = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetRoles", t => t.Id)
.Index(t => t.Id);
}
PS:在 AppUser 继承自 IdentityUser 的情况下,这按预期工作。这是脚本
为 IdentityUser 生成:
public class AppUser : IdentityUser
{
public string Description { get; set; }
}
public class AppUserMapping : EntityTypeConfiguration<AppUser>
{
public AppUserMapping():base()
{
this.Map(m =>
{
}).HasKey(e => e.Id);
}
}
public partial class initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.tb_AppUser",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Description = c.String(),
Email = c.String(),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(),
})
.PrimaryKey(t => t.Id);
}
覆盖 OnModelCreating
您的 Db 上下文 class 以将您的 AppRole table 绑定到 aspnetRoles。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AppRole>().ToTable("aspnetRoles");
// Commente below line if you got any errors.
modelBuilder.Entity<AppRole>().Property(r => r.Id);
}
希望对您有所帮助。
出现此行为的原因是因为我使用的是从 IdentityUser default 继承的 AppUser。
解决这个问题的方法是使用
AppUSer :IdentityUser<int,AppRole,AppUserRole,AppUserLogis,AppUserClaim>{
}
其中 AppRole、AppUserRole、AppUserLogis、AppUserClaim 是 IdentityRole 的自定义实体,
IdentityUserRole、IdentityUserLogin、IdentityUserClaim .
同样必须反映在 IdentityDBContext 中{
}
我正在尝试自定义 identityRole table 以添加新属性,但它正在创建新的 table 脚本而不是在脚本中创建属性。如何生成使用 db.tb_appRoles table 而不是 db.aspnetRoles 和 db.tb_appRoles 的脚本?
我的配置如下:
public class AppRole : IdentityRole
{
public string Description { get; set; }
}
public class AppRoleMapping : EntityTypeConfiguration<AppRole>
{
public AppRoleMapping()
: base()
{
Map(m =>
{
m.ToTable("tb_AppRole");
}).HasKey(e => e.Id); ;
}
}
// Generated script with two separate tables
public partial class initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"dbo.tb_AppRole",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Description = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.AspNetRoles", t => t.Id)
.Index(t => t.Id);
}
PS:在 AppUser 继承自 IdentityUser 的情况下,这按预期工作。这是脚本 为 IdentityUser 生成:
public class AppUser : IdentityUser
{
public string Description { get; set; }
}
public class AppUserMapping : EntityTypeConfiguration<AppUser>
{
public AppUserMapping():base()
{
this.Map(m =>
{
}).HasKey(e => e.Id);
}
}
public partial class initial : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.tb_AppUser",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Description = c.String(),
Email = c.String(),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(),
})
.PrimaryKey(t => t.Id);
}
覆盖 OnModelCreating
您的 Db 上下文 class 以将您的 AppRole table 绑定到 aspnetRoles。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<AppRole>().ToTable("aspnetRoles");
// Commente below line if you got any errors.
modelBuilder.Entity<AppRole>().Property(r => r.Id);
}
希望对您有所帮助。
出现此行为的原因是因为我使用的是从 IdentityUser default 继承的 AppUser。
解决这个问题的方法是使用
AppUSer :IdentityUser<int,AppRole,AppUserRole,AppUserLogis,AppUserClaim>{
}
其中 AppRole、AppUserRole、AppUserLogis、AppUserClaim 是 IdentityRole 的自定义实体, IdentityUserRole、IdentityUserLogin、IdentityUserClaim .
同样必须反映在 IdentityDBContext 中{ }