Samir Boulos
Samir Boulos

Reputation: 21

Exception thrown when using Entity Framework class library from ASP.NET Core 2.2 Web API project

I have 2 projects in my solution:

  1. An ASP.NET Core 2.2 Web API project
  2. A .net framework class library

In my class library I have created my entities using a database-first approach. In my Web API project, I am referencing the class library.

When I run the application, an exception gets thrown at this point:

return entities.IP_Types.ToList();

This is the exception below:

System.ArgumentException: '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.'

My app.config file seems to be correctly set up. I have the following code in it:

<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>

Upvotes: 1

Views: 287

Answers (2)

Samir Boulos
Samir Boulos

Reputation: 21

I managed to get over the exception by adding this line to the Program's Main method:

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

Upvotes: 1

Tejus
Tejus

Reputation: 724

Check your database connection string. Either its missing the providerName or the config file is missing the settings for entityFramework provider.

<entityFramework>
    <providers>
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
</entityFramework>

To know more about the connection configuration, Check out EF Database Providers

Upvotes: 0

Related Questions