No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.4.0'

后端 未结 12 2283
失恋的感觉
失恋的感觉 2020-12-05 22:44

I got the following error when I used sqlce 4.0 with entityframework 6.0

No Entity Framework provider found for the ADO.NET provider with invariant name \'Sy         


        
相关标签:
12条回答
  • 2020-12-05 23:36

    To achieve this you just need to register the EntityFramework.SqlServerCompact in Package Manager Console,

    To do this open power manager console and type below command:

    PM> Install-Package EntityFramework.SqlServerCompact
    
    0 讨论(0)
  • 2020-12-05 23:39

    I met the issue in my unit tests. The tricky thing was that the error had appeared not constantly. I managed to solve it by adding the follwing class my my unit tests solution:

    public static class Trick
    {
        public static void FixEfProviderServicesProblem()
        {
            // this commented line should be used for SQL Server provider
            //var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
    
            // this is used for SQL Server CE provider
            var instance = System.Data.Entity.SqlServerCompact.SqlCeProviderServices.Instance;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 23:40

    Don't forget that Package Manager Console takes app.config/web.config from selected as Startup project!

    0 讨论(0)
  • 2020-12-05 23:43

    I add the provider code below to the Web.config,and install the EntityFramework.SqlServerCompact package both, and then solve the problem.

    If lacking one, it fails.

    <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact"/>
    
    0 讨论(0)
  • 2020-12-05 23:46

    This error arose on a User PC. The user ignored a previous error "The ‘DbProviderFactories’ section can only appear once per config file".

    Apparently DB2 corrupted the machine config file on her PC, by adding a duplicate (and empty) <DbProviderFactories /> tag.

    The solution was to delete the empty duplicate tag. The machine config file is located in [WindowsDir]\Microsoft.Net\Framework[.NET Version]\Config\Machine.config

    0 讨论(0)
  • 2020-12-05 23:46

    the solution provided is correct but it does not explain why EF need this assembly. Here is Why...

    By default EF's configuration allow you to have a "defaultConnectionFactory" uder entityFramework section. This default factory is configured to "LocalDbConnectionFactory" and its param is "mssqllocaldb".

    This factory internally requires "SqlServerCe.4.0" i.e. "SqlServerCompact".

    The error happens only if the EF uses its default connection string in DbContext. If EF is provided with some other specified Connection string in your config then this error does not appear. Because EF by default uses the default connection factory.

    once you install "SqlServerCe 4.0" the configuration of EF is changed and "LocalDbConnectionFactory" and its param is "mssqllocaldb" are replaced with "SqlServer Ce 4.0". and EF runtime is also able to find the SqlServerCe4.0 Assembly (as it is now part of your references and physically placed in your BIN).

    Following is the EF Config before and after installing Sql Server Compact

    OLD Config:

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

    The new configuration is as below:

    <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="System.Data.SqlServerCe.4.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
          <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
        </providers>
      </entityFramework>
    

    and also it added the following section to describe new Factory

    <system.data>
        <DbProviderFactories>
          <remove invariant="System.Data.SqlServerCe.4.0" />
          <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
        </DbProviderFactories>
      </system.data>
    
    0 讨论(0)
提交回复
热议问题