带有 IdentityDbContext 和 hasData seeding 的 EFcore 创建 IdentityRole
EFcore with IdentityDbContext and hasData seeding creates IdentityRole
我有点眼花缭乱;
我正在使用 dotnet core (2.1) EF 进行迁移。当我仅根据上下文创建第一个迁移时,它实际上看起来一切正常。
所以我有这样的上下文:
public class DataContext : IdentityDbContext<User, IdentityRole<Guid>, Guid>
{
public virtual DbSet<Country> Countries { get; set; }
public virtual DbSet<CountryUser> CountryUsers { get; set; }
//..more stuff
public DataContext(DbContextOptions options) : base(options)
{ }
}
所以在创建它要添加的迁移之后:
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable:
//etc. etc. Removed rest of migrations.
我觉得一切都很好。
然后我开始关注下面的文章;
设置 HasData
并为数据库播种后,创建了一个新的迁移,它在要删除 AspNetRoles
table 的地方创建了一个新的迁移并创建为新的 table IdentityRole
。
我刚刚删除了数据库并尝试再次执行它,现在它只想在播种时插入数据。但我无法弄清楚我改变了什么,甚至更好,为什么它想要删除并创建不同的 tables.
有人可以解释它什么时候想要创建 AspNetRoles
以及什么时候想要创建 IdentityRoles
(以及随之而来的其他 table)。
只是为了确定;上下文总是从
延伸
: IdentityDbContext<User, IdentityRole<Guid>, Guid>
和我的 User
:
public class User : IdentityUser<Guid>
{
public virtual ICollection<CountryUser> CountryUsers { get; set; }
}
谢谢!!
据我了解,您正在遵循以下方法:
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() });
这段代码有两个问题。
首先是modelBuilder.Entity<IdentityRole>()
的用法。这使得 IdentityRole
class 被 EF Core 视为 实体 ,因此它尝试创建 IdentityRoles
table。这是因为 IdentityRole
class 与您的模型使用的 不同 - IdentityRole<Guid>
。改成modelBuilder.Entity<IdentityRole<Guid>>()
(还有new
运算符),问题就解决了。
第二个问题是 HasData
需要指定 PK 值,如 EF Core 文档的 Data Seeding 部分所述:
The primary key value needs to be specified even if it's usually generated by the database. It will be used to detect data changes between migrations.
因此,除了更改 HasData
通用类型参数之外,您还需要预先生成 Guid 以用作新角色的 Id(有关类似问题的更多详细信息,请参阅 )和使用这样的东西:
modelBuilder.Entity<IdentityRole<Guid>>().HasData(
new IdentityRole<Guid>
{
Id = new Guid("pre generated value"),
Name = "Admin",
NormalizedName = "Admin".ToUpper()
}
);
我有点眼花缭乱; 我正在使用 dotnet core (2.1) EF 进行迁移。当我仅根据上下文创建第一个迁移时,它实际上看起来一切正常。
所以我有这样的上下文:
public class DataContext : IdentityDbContext<User, IdentityRole<Guid>, Guid>
{
public virtual DbSet<Country> Countries { get; set; }
public virtual DbSet<CountryUser> CountryUsers { get; set; }
//..more stuff
public DataContext(DbContextOptions options) : base(options)
{ }
}
所以在创建它要添加的迁移之后:
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable:
//etc. etc. Removed rest of migrations.
我觉得一切都很好。
然后我开始关注下面的文章;
设置 HasData
并为数据库播种后,创建了一个新的迁移,它在要删除 AspNetRoles
table 的地方创建了一个新的迁移并创建为新的 table IdentityRole
。
我刚刚删除了数据库并尝试再次执行它,现在它只想在播种时插入数据。但我无法弄清楚我改变了什么,甚至更好,为什么它想要删除并创建不同的 tables.
有人可以解释它什么时候想要创建 AspNetRoles
以及什么时候想要创建 IdentityRoles
(以及随之而来的其他 table)。
只是为了确定;上下文总是从
延伸: IdentityDbContext<User, IdentityRole<Guid>, Guid>
和我的 User
:
public class User : IdentityUser<Guid>
{
public virtual ICollection<CountryUser> CountryUsers { get; set; }
}
谢谢!!
据我了解,您正在遵循以下方法:
modelBuilder.Entity<IdentityRole>().HasData(new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() });
这段代码有两个问题。
首先是modelBuilder.Entity<IdentityRole>()
的用法。这使得 IdentityRole
class 被 EF Core 视为 实体 ,因此它尝试创建 IdentityRoles
table。这是因为 IdentityRole
class 与您的模型使用的 不同 - IdentityRole<Guid>
。改成modelBuilder.Entity<IdentityRole<Guid>>()
(还有new
运算符),问题就解决了。
第二个问题是 HasData
需要指定 PK 值,如 EF Core 文档的 Data Seeding 部分所述:
The primary key value needs to be specified even if it's usually generated by the database. It will be used to detect data changes between migrations.
因此,除了更改 HasData
通用类型参数之外,您还需要预先生成 Guid 以用作新角色的 Id(有关类似问题的更多详细信息,请参阅
modelBuilder.Entity<IdentityRole<Guid>>().HasData(
new IdentityRole<Guid>
{
Id = new Guid("pre generated value"),
Name = "Admin",
NormalizedName = "Admin".ToUpper()
}
);