Entityframework.docs: System.TypeLoadException

Created on 19 Mar 2020  Â·  9Comments  Â·  Source: dotnet/EntityFramework.Docs

The sample code (the testing part) works perfectly in .net core 2.2, but in 3.1 it throws the following exception on the context.Database.EnsureCreated() line:

System.TypeLoadException : Could not load type 'Microsoft.EntityFrameworkCore.RelationalReferenceCollectionBuilderExtensions' from assembly 'Microsoft.EntityFrameworkCore.Relational, Version=3.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

closed-question

Most helpful comment

@behrooz66 That's due to a version mismatch. Check that the project references the same version of all EF packages.

All 9 comments

@behrooz66 That's due to a version mismatch. Check that the project references the same version of all EF packages.

@behrooz66 That's due to a version mismatch. Check that the project references the same version of all EF packages.

Going to try that and will update the results here :)

@behrooz66 That's due to a version mismatch. Check that the project references the same version of all EF packages.

Ok I can confirm that all the packages are on the same version and the exact same error is still occurring. Any other ideas?

@behrooz66 Please post your csproj file so we can take a look.

Hi @ajcvickers , thanks for responding. Here is the xUnit test project csproj where the issue occurs:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="3.1.2" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="WireMock.Net" Version="1.1.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\src\PNI.IdentityExpress.Service.SPLUS\PNI.IdentityExpress.Service.SPLUS.csproj" />
  </ItemGroup>

</Project>

And here is the csproj for the project that was referenced by the xUnit project;

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="12.0.2" />
    <PackageReference Include="PNI.IdentityExpress.Common" Version="1.1.1-rc0002" />
    <PackageReference Include="PNI.IdentityExpress.Service.Common" Version="1.0.1-rc0011" />
  </ItemGroup>

</Project>

@behrooz66 WireMock.Net has a lot of older dependencies; it could be causing the issue. I wasn't able to find the PNI.IdentityExpress packages, so I'm not sure what dependencies these pull in.

Well the PNI.IdentityExpress is an internal package we have developed and I can guarantee it doesn't have pretty much any dependencies to pull in. But I'm not sure about WireMock.Net. I am going to try a sample without it and will update the thread after.

Aside from that, nothing else seems to be setup the wrong way, true?

OK, I was able to resolve the issue however I don't get the logic. If I have this line in the csproj of the test project, it will fail:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.2" />

Removing it solves the problem.

@behrooz66 It's likely that your dependencies are bringing in an other EF Core and by removing that line you're no longer bringing in a new, conflicting version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidliang2008 picture davidliang2008  Â·  4Comments

weitzhandler picture weitzhandler  Â·  4Comments

jaxidian picture jaxidian  Â·  4Comments

SychevIgor picture SychevIgor  Â·  4Comments

mjehle82 picture mjehle82  Â·  3Comments