Reputation: 4645
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://learn.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 :-)
Upvotes: 2
Views: 1368
Reputation: 2759
Just so that this will be helpful for others. We did an upgrade to .NET 6.0 but didn't upgrade AutoMapper library. AutoMapper was 8.1.1 and AutoMapper.Extensions.Microsoft.DependencyInjection was 6.0.0. We got the same error. After debugging, it turned out the reason is because our API loads EntityFrameworkCore.CommonTools version 2.0.2 which references Microsoft.EntityFrameworkCore version 2.0.2 but because of .NET 6.0 upgrade, the process is loading Microsoft.EntityFrameworkCore version 6.0.14 and when AutoMapper tries to find all DefinedTypes of the Microsoft.EntityFrameworkCore version 2.0.2, one of them is Microsoft.EntityFrameworkCore.Query.Internal.IAsyncQueryProvider which was supposed to be in Microsoft.EntityFrameworkCore version 2.0.2 but now is moved to a different namespace in 6.0.14.
The workaround was to just not load the type by doing this instead
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies().Where(x
=> string.Equals(x.GetName().Name, "EntityFrameworkCore.CommonTools") == false));
Upvotes: 0
Reputation: 748
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.
Upvotes: 5