Connecting to SQL Server with EF6

2019-03-19 13:24发布

问题:

Up to EF5, in order to connect to SQL Server 2012, all I needed to to is specify a connection string that looks something like this:

Data Source=.\SqlExpress;Initial Catalog=MyDatabase;Integrated security=True;MultipleActiveResultSets=True

This method is not working with EF6, giving exception

No Entity Framework provider found for 'System.Data.Odbc' ADO.NET provider. Make sure the provider is registered in the 'entityFramework' section of the application config file

I am not using app.config file at all, I am passing above connection string to MyContext constructor. WHy is it trying to use Odbc provider at all, and instead not using System.Data.SqlClient?

What needs to be done to connect to SQL Server with EF6 code-first? I made sure that EntityFramework.dll and EntityFramework.SqlServer.dll are both available in Application folder. I have even added EF6 nuget package v6.0.0.1 in WPF project, although it does not use EF library directly, and making sure that automatically created App.Config file (by nuget) is copied to Application (Debug) folder - still no success.

I have also tried to setprovider manually in code:

public class OeeCoachConfiguration : DbConfiguration
{
    public OeeCoachConfiguration()
    {
        SetProviderServices("System.Data.SqlClient",
            System.Data.Entity.SqlServer.SqlProviderServices.Instance);
    }
}

Still no success. My Project structure is as follows (simplified):

WPF project - does not have reference to EF (also tried adding EF reference)

ViewModel class library - does not have reference to EF

Model class library - has reference to EF library (both dlls)

Data class library - has reference to UI library (both dlls).

I am using CodeFirst approach, and this setup works without any problem with EF5. Any help is greatly appreciated.

回答1:

For me this error was resolved by removing the Glimpse.ADO package

Edit December 20, 2013 I believe the Glimpse.ADO package now supports EF6, but I have not tested it.



回答2:

I had the same problem and I eventually tried the following and it worked (keeping Glimpse EF5 & Glimpse ADO packages)

In the web.config I added a copy of the existing provider line but changed it's invariantName to "System.Data.Odbc".

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.Odbc" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

Hope that this helps.

Jonathan



回答3:

Yes, for me it was resolved by removing Glimpse.ADO and Glimpse.EF5 nuget packages. Thank you.



回答4:

Option 1: Try adding following in app.config of the executable project.

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

Option 2: If you want to use code based provider registration, follow guideline on Entity Framework Providers for EF6

  • Make sure you have the DbConfiguration in the same assembly as the DbContext of your application.