我的Configure
方法中包含以下代码,该代码在运行程序时支持执行EF Core迁移。它确实运行没有错误并且能够创建数据库,但是数据库中没有创建表。我尝试了context.Database.EnsureCreated()
和context.Database.Migrate()
,但结果相同。
public void Configure(IApplicationBuilder应用程序,IWebHostEnvironment env) { 使用(var serviceScope = app.ApplicationServices.GetRequiredService()。CreateScope()) { var context = serviceScope.ServiceProvider.GetService(); context.Database.EnsureCreated(); //context.Database.Migrate(); }
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
这是我的上下文类
public class AppDbContext : DbContext
{
public AppDbContext (DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Magazine> Magazine { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Magazine>().HasData(
new Magazine { MagazineId = 1, Name = "MSDN Magazine" },
new Magazine { MagazineId = 2, Name = "Docker Magazine" },
new Magazine { MagazineId = 3, Name = "EFCore Magazine" }
);
}
}