问题
When running all the test, I'm eventually getting errors saying:
"An item with the same key has already been added. Key: 125"
This doesn't happen when each test is run separately.
The funny thing is that each test works with a different DbName, to avoid any conflict:
[TestMethod]
public void Test1()
{
using (var context = CreateTestingContext())
{
...
}
}
[TestMethod]
public void Test2()
{
using (var context = CreateTestingContext())
{
...
}
}
protected static SGDTPContext CreateTestingContext([CallerMemberName] string dbName = "TestingDb")
{
var builder = new DbContextOptionsBuilder<MyDbContext>().UseInMemoryDatabase(dbName);
return new MyDbContext(builder.Options);
}
It's really strange, because when I run the tests individually, they are Green! and when I run them all at the same time, some eventually fail.
NOTE: I'm using the integrated MSTest from Visual Studio 2017.
来源:https://stackoverflow.com/questions/47335920/entity-framework-core-inmemory-database-tests-break-when-run-in-parallel