【发布时间】:2017-08-13 05:10:53
【问题描述】:
我在我的 ASP.NET 核心解决方案中实现了数据库迁移,正如以下问题中所推荐的那样:Pattern for seeding database with EF7 in ASP.NET 5
我的解决方案是为在 linux docker 上工作而设置的,该应用程序依赖于在 docker compose 文件中配置的 MySql 容器,并在第一次运行时设置。
迁移在 Startup.Configure 方法中运行为:
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetService<ApplicationDbContext>();
context.Database.Migrate();
context.EnsureSeedData();
}
但是第一次运行应用程序总是会抛出以下错误:
System.Private.CoreLib.ni.dll 中出现“MySql.Data.MySqlClient.MySqlException”类型的异常,但未在用户代码中处理
然后,如果我等待几秒钟并重新启动调试会话,代码将毫无问题地执行,并且首次运行的数据就在那里。
有没有办法在运行迁移之前等待数据库服务器准备好?
编辑:
如果我更改此问题中的迁移方法:Cannot get the UserManager class 而不是上一个错误,我得到了这个:
System.Private.CoreLib.ni.dll 中出现“System.AggregateException”类型的异常,但未在用户代码中处理
【问题讨论】:
标签: mysql docker asp.net-core