简体   繁体   English

EF Core EnsureCreated() 仅创建第一个 DbContext

[英]EF Core EnsureCreated() only creates the first DbContext

I'm having an interesting issue were two EnsureCreated() calls only create the first DbContext .我有一个有趣的问题是两个EnsureCreated()调用只创建第一个DbContext

The issue only happens on my tests, because my default database were done with Migrations.这个问题只发生在我的测试中,因为我的默认数据库是使用迁移完成的。

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();
            }
        });
    }
}

This is my WebApplicationFactory, that gets called by tests.这是我的 WebApplicationFactory,它被测试调用。
The issue only happened at CI, a new SQL Server was created on docker, and only the "dbRefresh" table were added.问题只发生在 CI,在 docker 上创建了一个新的 SQL 服务器,并且只添加了“dbRefresh”表。 No sign, or error of the "dbUser" table. “dbUser”表没有符号或错误。
It's just not there, and breaks only when the tests hits it.它只是不存在,只有在测试命中时才会中断。
Both DbContext uses the same connection string in the tests.两个DbContext在测试中使用相同的连接字符串。

"Both DbContext uses the same connection string in the tests." “两个 DbContext 在测试中使用相同的连接字符串。”

The Database name is in the connection string.数据库名称在连接字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM