Search code examples
c#entity-framework-corein-memory-database.net-7.0ef-core-7.0

EF 7 - new ExecuteDelete and ExecuteUpdate methods not working on an in-memory database


I'm using the new EF 7 ExecuteDelete and ExecuteUpdate features and they are great. But when I try to write unit test for the functions that use them, these tests crash.

I'm using EF 7.0.1 on .NET Core 7, Microsoft.EntityFrameWorkCore.InMemory 7.0.1, and Xunit 2.4.2

[Fact]
public async Task DeleteAccountAsync__Success()
{
    var accountId = Guid.Parse("52ff9d4e-efb9-4b51-b5e7-1734d10187f7");

    // CRASH
    _context.Accounts
            .Where(p => p.AccountId == accountId)
            .ExecuteDelete();
}

private void SeedDataBase()
{
    var a1 = new Account
        {
            AccountId = Guid.Parse("52ff9d4e-efb9-4b51-b5e7-1734d10187f7"),
        };

    var a2 = new Account
        {
            AccountId = Guid.Parse("d5c38300-0de6-4118-8d01-6bc94842d4b5"),
        };

    _context.Accounts.AddRange(a1, a2);
    _context.SaveChanges();
}

This is the error I get:

Message "The LINQ expression 'DbSet()\n .Where(a => a.AccountId == __accountId_0)\n .ExecuteDelete()' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.


Solution

  • These new extensions only work on relational providers. And InMemory is not a relational provider