11

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ivo Oostwegel
  • 374
  • 2
  • 20
  • 2
    Using EF in memory provider for testing is usually not a good idea. See https://learn.microsoft.com/en-us/ef/core/providers/in-memory/?tabs=dotnet-core-cli. "While some users use the in-memory database for testing, this is generally discouraged; the SQLite provider in in-memory mode is a more appropriate test replacement for relational databases" – Evk Dec 24 '22 at 12:35

2 Answers2

12

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

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
0

Even when using sqlite, ExecuteUpdate is not working in EF.Core.Sqlite 7.02. ExecuteDelete works and ExecuteUpdate throws no errors, but it doesn't actually update records.

  • 1
    This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/33718039) – as-if-i-code Jan 31 '23 at 07:44