Cannot register IRelationalTypeMappingSource

Viewed 11592

System.InvalidOperationException: 'The database provider attempted to register an implementation of the 'IRelationalTypeMappingSource' service. This is not a service defined by Entity Framework and as such must be registered as a provider-specific service using the 'TryAddProviderSpecificServices' method.'

This is the exception when I run page with DbContext in.

The website is Razor Pages.

Website was working well a I did not change anything a now this happened.

Here is .csproj

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.10">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.0" />
  </ItemGroup>

</Project>

Does anyone know how to fix this issue?

3 Answers

You are mixing versions, and referencing too many packages. change to this:

<ItemGroup>
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="1.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.10">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
    <PackageReference Include="Microsoft.Windows.Compatibility" Version="3.1.1" />
  </ItemGroup>

I had the same issue, and fix it like so:
In the file (Project).csproj, I comment these rows.

PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="5.0.0"

PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.6" 

PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5"

This will work properly

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

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

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.21" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.21">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
  </ItemGroup>

</Project>
Related