![](/img/trans.png)
[英]Confusing Behavior of DbContext.Database.EnsureCreated in EF Core 2.1
[英]EF Core EnsureCreated() only creates the first DbContext
我有一个有趣的问题是两个EnsureCreated()
调用只创建第一个DbContext
。
这个问题只发生在我的测试中,因为我的默认数据库是使用迁移完成的。
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var sp = services.BuildServiceProvider();
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var dbUser = scopedServices.GetRequiredService<UserTrackerContext>();
var dbRefresh = scopedServices.GetRequiredService<RefreshTokenContext>();
dbRefresh.Database.EnsureCreated();
dbUser.Database.EnsureCreated();
}
});
}
}
这是我的 WebApplicationFactory,它被测试调用。
问题只发生在 CI,在 docker 上创建了一个新的 SQL 服务器,并且只添加了“dbRefresh”表。 “dbUser”表没有符号或错误。
它只是不存在,只有在测试命中时才会中断。
两个DbContext
在测试中使用相同的连接字符串。
“两个 DbContext 在测试中使用相同的连接字符串。”
数据库名称在连接字符串中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.