efcore.pg icon indicating copy to clipboard operation
efcore.pg copied to clipboard

Could not load type 'Npgsql.Internal.TypeMapping.IUserEnumTypeMapping'

Open Giorgi opened this issue 2 years ago • 6 comments

I'm upgrading my EntityFramework.Exceptions project to .NET 8 and EF Core 8 (multi-targeting .net6 as well) and I get an error when trying to run my tests against .NET 6. As .Net 6 doesn't support EF Core 8, in the Common project that implements database agnostic logic, I have EF references based on target framework:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" Condition="'$(TargetFramework)' == 'net6.0'" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0"  Condition="'$(TargetFramework)' == 'net8.0'" />

In the PostgreSQL project I reference <PackageReference Include="Npgsql" Version="8.0.1" /> because it targets both .net6 and .net8.

The Test project references both of these projects as well as EF Core and database provider specific packages:

<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.14" />
  <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net8.0'">
  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0" />
  <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>

When I run tests against .Net 8, everything works fine but when running against .Net 6 postgres tests fail with this error:

System.TypeLoadException Could not load type 'Npgsql.Internal.TypeMapping.IUserEnumTypeMapping' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'. at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlTypeMappingSource.SetupEnumMappings(ISqlGenerationHelper sqlGenerationHelper, NpgsqlDataSource dataSource) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlTypeMappingSource.LoadUserDefinedTypeMappings(ISqlGenerationHelper sqlGenerationHelper, NpgsqlDataSource dataSource) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlTypeMappingSource..ctor(TypeMappingSourceDependencies dependencies, RelationalTypeMappingSourceDependencies relationalDependencies, ISqlGenerationHelper sqlGenerationHelper, INpgsqlSingletonOptions options) at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)

Full output from CI is available here: https://ci.appveyor.com/project/Giorgi/entityframework-exceptions

Giorgi avatar Dec 09 '23 13:12 Giorgi

@Giorgi are you sure you're not accidentally mixing major versions of EFCore.PG and Npgsql? This error looks like it would happen if e.g. EFCore.PG 7 were used against Npgsql 8.

roji avatar Dec 11 '23 05:12 roji

I made the following change in the Postgres project and now it works:

<PackageReference Include="Npgsql" Version="7.0.6" Condition="'$(TargetFramework)' == 'net6.0'" />
<PackageReference Include="Npgsql" Version="8.0.1" Condition="'$(TargetFramework)' == 'net8.0'" />

I thought EFCore.PG 7.0.11 would work with Npgsql 8.0.1 because if I go to https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL/7.0.11#dependencies-body-tab it says Npgsql (>= 7.0.6) and doesn't restrict it by an upper bound like it does for other dependencies.

Giorgi avatar Dec 11 '23 06:12 Giorgi

Unfortunately not - some of the big type mapping changes in Npgsql 8.0 mean that EFCore.PG 7.0 is incompatible with Npgsql 8.0. I'll take a look to see if they can be easily be made compatible, and if not, I'll update the nuget dependency to reflect that.

In the meantime, you'll have to align major versions.

roji avatar Dec 11 '23 10:12 roji

Thanks for the details, Shay.

Giorgi avatar Dec 11 '23 10:12 Giorgi

Basically, what should we do? I'm confused, I've installed Npgsql.EntityFrameworkCore.PostgreSQL version 8.0 to be compatible with the entity framework but it doesn't work

alpemreelmas avatar Jun 13 '24 17:06 alpemreelmas

Unfortunately I don't think we'll be making EFCore.PG 7.0 compatible with Npgsql 8.0, the changes are too significant and EF 7.0 is now out of support.

In general, align the major versions of EFCore.PG and Npgsql - these are the versions which are developed and tested together. In principle it should be possible to mix major versions, but we sometimes make type mapping changes etc. which make different major versions incompatible.

I'm confused, I've installed Npgsql.EntityFrameworkCore.PostgreSQL version 8.0 to be compatible with the entity framework but it doesn't work

If you have a problem when using Npgsql.EntityFrameworkCore.PostgreSQL 8.0 with Npgsql 8.0, that's unrelated to this issue; please open a new issue with a minimal, runnable repro.

roji avatar Jun 14 '24 11:06 roji