efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Ef core (inmemory) issue when running all tests at once

Open chinookproject opened this issue 7 years ago • 1 comments

I have a unit test project where I use the InMemory database. In that project I have three test methods. In all methods I new all classes, including my DbContext. Each DbContext has its own databaseName per test method.

Much like the docs says how to set everything up.

When I run each test individually, then it all works fine. When I run all tests at once (in parallel I guess? I'm using xunit), then only one test passes, the other two fail.

It doesn't seem like a race condition. For some reason it seems like the DbContext in the other two test methods are not saving any changes.

I have added a simplified version of our project as an attachment. You should be able to reproduce the issue with this. EfCoreError.zip

I am really wondering, am I doing something wrong here..? Or is this a bug in EF Core?

chinookproject avatar Apr 02 '19 21:04 chinookproject

@chinookproject What's happening here is that ProductAfname is using a generated key value. The key generator for in-memory databases is shared by multiple entity types, which means if multiple tests are run, then the key values for entities can be different compared to the case where only a single test is run. We have improved this a lot for EF Core 3.0--see #6872

The workarounds for now are to either explicitly specify the key value in the test, or to read back the generated key value and use this in the assertions.

ajcvickers avatar Apr 03 '19 16:04 ajcvickers