I'm using Entity Framework Core 2.0-preview1
with InMemory 2.0-preview1
.
Each unit test class inherits a disposable class that creates a new in memory database that its parents can use.
public Constructor()
{
var services = new ServiceCollection();
services.AddEntityFrameworkInMemoryDatabase()
.AddDbContext<DBContext>(o => o.UseInMemoryDatabase("Test"));
var serviceProvider = services.BuildServiceProvider();
Context = serviceProvider.GetRequiredService<DBContext>();
}
The issue with giving the database a name is that it cannot be shared across multiple tests and thus each test creates a new context resulting in each unit test lasting a few seconds which is unacceptable for my build server. I cant find much documentation on why this was changed in 2.0 or how to get past this.
I've tried using the new .UseTransientInMemoryDatabase
but this appears to change nothing. Thanks in advance.
I used a xUnit fixture to provide all my test instances with the save database context. That way I avoid the context creation overhead on each test which speeds the build server up by a large margin.