Reputation: 3
For example, when adding migration; When adding migrations like mig 1, mig 2, mig_3, the seed data (same data) is added to the database again and again during each addition, can I prevent this and add only one seed data data once and add as many migrations as I want?
.Net Core Web Api
public async void Configure(EntityTypeBuilder<AppRole> builder)
{
AppRole role1 = await _roleManager.FindByNameAsync("USER");
AppRole role2 = await _roleManager.FindByNameAsync("ADMIN");
AppRole role3 = await _roleManager.FindByNameAsync("SUPER USER");
if (role1 != null && role2 != null && role3 != null)
{
return;
}
builder.HasData(
new AppRole { Id = Guid.NewGuid().ToString(), Name = "super user", NormalizedName = "SUPER USER" },
new AppRole { Id = Guid.NewGuid().ToString(), Name = "admin", NormalizedName = "ADMIN" },
new AppRole { Id = Guid.NewGuid().ToString(), Name = "user", NormalizedName = "USER" }
);
}
Upvotes: 0
Views: 171
Reputation: 1186
There are some limitations on model seeding data.
In your case you are creating a key each time using Guid.NewGuid().
Alternatively, you can use context.Database.EnsureCreated() to create a new database containing the seed data, for example for a test database or when using the in-memory provider or any non-relational database. Note that if the database already exists, EnsureCreated() will neither update the schema nor seed data in the database. For relational databases you shouldn't call EnsureCreated() if you plan to use Migrations
Upvotes: 0