答案 0 :(得分:0)
为什么它的价值migrations
不能与EnsureCreatedAsync一起运行,它在EFCore的文档中用真正的粗体字母表示,也可以用种子方法绕过这个Run EnsureDeletedAsync
(无论如何都要测试)对?)。然后运行Database.MigrateAsync();
https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/#applying-migrations-at-runtime
所以......
修改强>
//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...
//...
}
}