'EntityFramework Core 6 upgrade Could not load type IAsyncQueryProvider

I've updated my .NET Core 3.1 application to .NET 6 and one step was to also update EntityFramwork Core to version 6.0.2.

Unfortunately, many of my unit tests are now failing with the exception:

System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Query.Internal.IAsyncQueryProvider' from assembly 'Microsoft.EntityFrameworkCore, Version=6.0.2.0,

I'm not really sure why this is happening. I've gone through the migration guide (https://docs.microsoft.com/en-us/aspnet/core/migration/31-to-60?view=aspnetcore-6.0&tabs=visual-studio) and haven't seen anything like this mentioned.

Here is one example of a unit test that fails with this exception:

[TestMethod]
public async Task IsPrivatKundeAsync_WithMatchingCriteria_ReturnsTrue()
{
    var data = new[] {
        new MyEntity { Partnernr = "1", Geburtstag = new DateTime(2000, 1, 1), Plz = "8000" },
        new MyEntity { Partnernr = "2", Geburtstag = new DateTime(2001, 2, 2), Plz = "8001" },
        new MyEntity { Partnernr = "3", Geburtstag = new DateTime(2002, 3, 3), Plz = "8002" },
    };

    var builder = new DbContextOptionsBuilder<MyDbContext>().UseInMemoryDatabase(Guid.NewGuid().ToString());
    var db = new Mock<MyDbContext>(builder.Options);
    db.Setup(x => x.MyEntity).Returns(data.AsQueryable().BuildMockDbSet().Object);

    IPartner partner = new Partner(db.Object);
    var result = await partner.IsPrivatKundeAsync(data[1].Partnernr, data[1].Geburtstag.Value, data[1].Plz);

    Assert.IsTrue(result);
}

These tests have been working before the update.

Thanks for any insights :-)



Solution 1:[1]

The issue most likely is in one of the packages that you use for testing.

EF Core changed the namespace of this class from Microsoft.EntityFrameworkCore.Query.Internal.IAsyncQueryProvider to Microsoft.EntityFrameworkCore.Query.IAsyncQueryProvider between ef core versions 3 to 5 and it means that one of the test packages either needs to be updated or changed. In my case it was MockQueryable.Moq package.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 merlinabarzda