Using different InMemory databases still increases identity

c# entity-framework-core

Question

I'm setting up my test framework to use an in memory database for the different scenarios. Each test uses a "unique" database, and each step of the test use a different context for entity framework.

Problem is, even if the tests use different "in memory" databases, and different contexts from entity framework, the identity in the database seems to increase.

For example, I have 2 unit tests:

[Test]
public async Task CanGetAllFromRepository()
{
    var options = new DbContextOptionsBuilder<MyContext>()
        .UseInMemoryDatabase(databaseName: "CanGetAllFromRepository").Options;
    var testa = new User();
    var testb = new User();
    var testc = new User();

    using (var context = new MyContext(options))
    {
        await context.Users.AddAsync(testa);
        await context.Users.AddAsync(testb);
        await context.Users.AddAsync(testc);
        await context.SaveChangesAsync();
    }

    using (var context = new MyContext(options))
    {
        var repo = new UserRepository(context);
        var all = await repo.GetAllAsync();
        Assert.IsTrue(all.Count == 3);
    }
}

[Test]
public async Task CanGetByIdFromRepository()
{
    var options = new DbContextOptionsBuilder<MyContext>()
        .UseInMemoryDatabase(databaseName: "CanGetByIdFromRepository").Options;
    var testa = new User();
    var testb = new User();
    var testc = new User();

    using (var context = new SalesPortalContext(options))
    {
        await context.User.AddAsync(testa);
        await context.User.AddAsync(testb);
        await context.User.AddAsync(testc);
        await context.SaveChangesAsync();
    }

    using (var context = new MyContext(options))
    {
        var repo = new UserRepository(context);
        var usera = await repo.GetByIdAsync(1);
        var userb = await repo.GetByIdAsync(2);
        var userc = await repo.GetByIdAsync(3);

        Assert.IsNotNull(usera);
        Assert.IsNotNull(userb);
        Assert.IsNotNull(userc);
    }
}

If i run the tests 1 by 1, its not an issue, but running them all together; then for the 2nd unit test the identity set ids of the users start 4, 5, 6. (When run individually, its 1, 2, 3 as expected).

UPDATE

Ive managed to get around the issue, by manually assigning the Users their IDs, as such:

var testa = new User() { Id = 1 };
var testb = new User() { Id = 2 };
var testc = new User() { Id = 3 };
1
3
9/14/2018 7:13:12 AM

Accepted Answer

This seems to be an open issue on the EF github: https://github.com/aspnet/EntityFrameworkCore/issues/6872

0
9/15/2018 9:35:25 AM

Popular Answer

I do same thing with XUnit and this is work for me. XUnit run the constructor for each test and there is no problem when initialize the context. Try using it



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow