I don't want to seed if the data exist in the modelBuilder
.
Here is I tried and seeded again with different role ids
so my user with the roles has gone.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<IdentityRole>().HasData(
new IdentityRole { Name = "Admin", NormalizedName = "Admin".ToUpper() },
new IdentityRole { Name = "PoUser", NormalizedName = "PoUser".ToUpper() },
new IdentityRole { Name = "User", NormalizedName = "User".ToUpper() }
);
}
So how can I prevent data seeding with conditional rendering in Asp.Net Core 3.1 if data exist in the db ?
Thanks
It seems you must add a primary key into the model.
builder.Entity<IdentityRole>().HasData(
new IdentityRole { Id = 1, Name = "Admin", NormalizedName = "Admin".ToUpper() },
new IdentityRole { Id = 2, Name = "PoUser", NormalizedName = "PoUser".ToUpper() },
new IdentityRole { Id = 3, Name = "User", NormalizedName = "User".ToUpper() }
);
Because it will be used to detect data changes between migrations.
About Data Seeding.
Hope this could helps.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments