简体   繁体   中英

EF Core EnsureCreated() only creates the first DbContext

I'm having an interesting issue were two EnsureCreated() calls only create the first 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.
The issue only happened at CI, a new SQL Server was created on docker, and only the "dbRefresh" table were added. No sign, or error of the "dbUser" table.
It's just not there, and breaks only when the tests hits it.
Both DbContext uses the same connection string in the tests.

"Both DbContext uses the same connection string in the tests."

The Database name is in the connection string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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