The specified invariant name 'System.Data.sqlClient' wasn't found in the list of registered .NET data providers in an ASP.NET Core Web API

Viewed 732

I am developing ASP.NET Core Web API for an ASP.NET MVC project. So, all services are written based on MVC and I should only call them, however, I come across the above error. I have tried the below codes, unfortunately, none of them worked.

I used RegisterFactory in the Configure method of the Startup class to add the invariantName:

DbProviderFactories.RegisterFactory("System.Data.SqlClient", System.Data.SqlClient.SqlClientFactory.Instance);

I then added the provider to appsetting.json:

"entityFramework": {
    "providers": {
        "provider": {
            "invariantName": "System.Data.SqlClient",
            "type": "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"
        }
    }
}

Some additional information:

  1. The .NET Core version is 2.2
  2. The EF version is 6.0.0
  3. The Microsoft.EntityFrameworkCore.Tools version is 3.1.20
1 Answers

You can restore some EntityFrameworkCorenuget packages to 2.0.0.

It seems to have solved this problem for me.

These are the packages I downgraded:

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
Related