Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Core 2.1 In memory DB not updating records

I'm using the in memory database provider for integration tests however I don't seem to be able to update a record. I've run the same code against a real SQL database and everything gets updated fine. Here is my test fixture code.

Test Fixture:

public class TestFixture<TStartup> : IDisposable
{
    private readonly TestServer _testServer;
    public HttpClient TestClient { get; }
    public IDatabaseService DbContext { get { return _testServer.Host.Services.GetService<DatabaseService>(); } }

    public TestFixture() : this(Path.Combine("src")) { }

protected TestFixture(string relativeTargetProjectPatentDir)
{
    Environment.SetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", "Testing");

    var builder = new WebHostBuilder()
        .ConfigureServices(services =>
        {
            services.AddDbContext<DatabaseService>(options =>
                options.UseInMemoryDatabase("TestDB")
                .EnableSensitiveDataLogging());
        })
        .UseEnvironment("Testing")
        .UseStartup<Startup>();

    _testServer = new TestServer(builder)
    {
        BaseAddress = new Uri("http://localhost:5010")
    };

    TestClient = _testServer.CreateClient();
    TestClient.BaseAddress = _testServer.BaseAddress;
}

    public void Dispose()
    {
        TestClient.Dispose();
        _testServer.Dispose();
    }
}

I've spent most of the day googling this and not come across any other people talking about it so I'm assuming its probably my issue rather than a EF bug. I'm sure someone would have noticed a DB that you can't update.

like image 733
Chris Sainty Avatar asked May 25 '18 14:05

Chris Sainty


2 Answers

Updating works with Singleton but I have CQRS architecture and to check if the entry was updated in e2e test I have to reload entry

Context.Entry(entity).Reload();

I hope that this can help someone

like image 55
ZeSzymi Avatar answered Sep 28 '22 03:09

ZeSzymi


It turned out that changing the lifetime of my DbContext in my test fixture to singleton solved my issue.

like image 43
Chris Sainty Avatar answered Sep 28 '22 03:09

Chris Sainty