Search code examples
.netpostgresqlvisual-studiodllnpgsql

The type initializer for 'Npgsql.TypeMapping.GlobalTypeMapper' threw an exception


Using Visual Studio 2017 try using the Server Explorer to add a connection to a Postgres database. Click Test connection. The issue An error is shown with: The type initializer for 'Npgsql.TypeMapping.GlobalTypeMapper' threw an exception.

Before that I installed System.Runtime.CompilerServices.Unsafe v4.5.2 and System.Numerics.Vectors v4.4.0 to fix the error message "Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1" and Could not load file or assembly 'System.Numerics.Vectors, Version=4.1.3.0.

I have no idea to fix this "The type initializer for 'Npgsql.TypeMapping.GlobalTypeMapper' threw an exception."

I just want to add the connection of PostgreSQL database to Visual Studio 2017. I already have a database in postgres... I don't know what I'm going to install first to add that connection.

After I installed Npgsql PostgresSQL Integration in Visual Studio=>Tools=> Extension and update, I created new project and go directly to Manage Nuget Packages and installed System.Runtime.CompilerServices.Unsafe v4.5.2 and System.Numerics.Vectors v4.4.0. And I directly tried to add the connection in Server Explorer. I have no idea into this. I just want to add the data connection of postgres in visual studio so that I can add ADO.NET Entity Data model.

Further technical details .NET framework 4.6.1 Npgsql version 4.1.1.2


Solution

  • Here is one potential fix that I found. It is hard to understand exactly what this does, but it works! You can add this to your csproj file within the root . Found this via here. This seems to be kind of a hack. Not sure if there is a solution to the root of the problem.

    <Target Name="ReplaceRefWithLib" BeforeTargets="ResolveAssemblyReferences">
        <ItemGroup>
          <_noCopyRefs Include="@(Reference)" Condition="'%(Reference.Private)' == 'false'" />
          <_noCopyRefsByFileName Include="@(_noCopyRefs->'%(FileName)')">
            <OriginalItem>%(Identity)</OriginalItem>
          </_noCopyRefsByFileName>
    
          <_libByFileName Include="@(ReferenceCopyLocalPaths->'%(FileName)')">
            <OriginalItem>%(Identity)</OriginalItem>
          </_libByFileName>
    
          <_overlappingRefByFileName Include="@(_noCopyRefsByFileName)" Condition="'@(_noCopyRefsByFileName)' == '@(_libByFileName)' AND '%(Identity)' != ''" />
          <_overlappingLibByFileName Include="@(_libByFileName)" Condition="'@(_noCopyRefsByFileName)' == '@(_libByFileName)' AND '%(Identity)' != ''" />
    
          <_overlappingRef Include="@(_overlappingRefByFileName->'%(OriginalItem)')" />
          <_overlappingLib Include="@(_overlappingLibByFileName->'%(OriginalItem)')" />
        </ItemGroup>
    
        <ItemGroup Condition="'@(_overlappingRef)' != ''">
          <Reference Remove="@(_overlappingRef)" />
          <Reference Include="@(_overlappingLib)">
            <Private>false</Private>
          </Reference>
        </ItemGroup>
      </Target>
    
      <Target Name="RemoveNetFxForceConflicts" AfterTargets="ResolveAssemblyReferences">
        <ItemGroup>
          <ReferencePath Remove="@(ReferencePath)" Condition="'%(FileName)' == 'netfx.force.conflicts'" />
        </ItemGroup>
      </Target>