1

I develop a web api project with ASP.NET. I want to establish a connection between ADO.NET and the databases I have added, but I keep encountering this error:

The ADO.NET provider with invariant name 'System.Data.SqlClient' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details.

I searched the internet for a solution but couldn't find it. The solutions in this thread didn't work: Entity Framework ADO.NET Sql.Data.Client provider not found.

What should I do?

  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MyModel1Entities" connectionString="metadata=res://*/Models.MyModel1.csdl|res://*/Models.MyModel1.ssdl|res://*/Models.MyModel1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MyModel1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="MyModel2Entities" connectionString="metadata=res://*/Models.MyModel2.csdl|res://*/Models.MyModel2.ssdl|res://*/Models.MyModel2.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=MyModel2;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
Alexey
  • 2,439
  • 1
  • 11
  • 15
k4mil
  • 11
  • 3
  • Is this a brand new project? If so, why are you using the old Web API in the old ASP.NET in the old .NET Framework? That's basically a dead end path: use Entity Framework Core on ASP.NET Core MVC on ASP.NET Core on .NET 6 for current development. – mason Aug 03 '22 at 17:03
  • Yes, it's a new project. I did it with ASP.NET Core Web API. With Core 3.1 version. Is switching to MVC really a solution? – k4mil Aug 03 '22 at 17:09
  • Web API is part of ASP.NET Core MVC, so there's not really an "ASP.NET Core Web API". The code you've shown looks to be something that would go in a web.config file: that's not how you configure Entity Framework in .NET Core 3.1 or .NET 6. The only role web.config still plays for ASP.NET Core is to configure ASP.NET Core Hosting Module. It appears you may have been following some old tutorials or something, as [Entity Framework Core](https://www.nuget.org/packages/Microsoft.EntityFrameworkCore/6.0.7) has replaced Entity Framework, and uses Microsoft.Data.SqlClient, not System.Data.SqlClient. – mason Aug 03 '22 at 17:13
  • I suggest you start with a [modern tutorial](https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio) and go from there. – mason Aug 03 '22 at 17:15
  • The code I am showing is from the Models' App.Config file. I will move the project as you said. I hope it works. Thanks for your reply – k4mil Aug 03 '22 at 17:17
  • App.Config wouldn't exist in an ASP.NET Core project. If you've put this in a class library, that class library should target .NET Standard, .NET Core 3.1, or .NET 6. And it wouldn't have an app.config. – mason Aug 03 '22 at 17:19

0 Answers0