I am using migrations and also want to use seed data. Currently getting error. what is the best way to achieve this?
My earlier query in stackoverflow: previous post
Thanks
I am using migrations and also want to use seed data. Currently getting error. what is the best way to achieve this?
My earlier query in stackoverflow: previous post
Thanks
For what ever its worth migrations
do not run with EnsureCreatedAsync, its in real big bold letters in the docs for EFCore, also to get around this Run EnsureDeletedAsync
with in your seed method (testing anyway right?). Then run Database.MigrateAsync();
so....
Edit
//example method
public void Seed(IApplicationBuilder app)
{
using(var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = servicescope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Database.Migrate();
if(!context.SomeTable.Any()){
foreach(var item in SeedData.Items){
context.SomeTable.Add(item);
}
context.SaveChanges();
}
//for each table you need seeded data...
//...
}
}