EF 6和SQLite 1.0.96.0出现“No Entity Framework provider found”错误

45

我知道关于这个主题已经有几个类似的问题了,但是很多问题都来自旧版本的SQLite,据我所知它们并不完全支持EF 6。我尝试过许多来自这些线程的建议,但我可能做错了什么或者某些事情已经改变了。

我正在使用VS 2013,目标是.NET 4.5.1,并从system.data.sqlite.org下载页面安装了sqlite-netFx451-setup-bundle-x86-2013-1.0.96.0.exe包,以及从NuGet管理器安装了System.Data.SQLite EF6包(它安装了EF6)。

以下是我的当前App.config文件(它基本上没有被修改,除了我尝试向类型添加了Version、Culture和Public key变量):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
  </startup>
  <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" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.96.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6, Version=1.0.96.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
      />
    </DbProviderFactories>
  </system.data>
</configuration>

我的 packages.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.2" targetFramework="net451" />
  <package id="System.Data.SQLite.EF6" version="1.0.96.0" targetFramework="net451" />
</packages>

如果我尝试从模型生成数据库,会出现以下错误:

  

找不到名为'System.Data.SQLite.EF6'的ADO.NET提供程序的Entity Framework提供程序。 确保在“entityFramework”部分中注册了提供程序...

我已经尝试修改App.config文件并添加其他提供程序,就像旧帖子建议的那样,但都没有成功。

如何解决这个问题? 非常感谢任何帮助!

编辑:我设法让它工作得足够好,以使用数据库优先方法。 这是我的App.config文件的相关部分:

<providers>
     <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>

<DbProviderFactories>
     <remove invariant="System.Data.SQLite" />
     <remove invariant="System.Data.SQLite.EF6" />
     <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
     <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>

我正在使用EF 6.1.2和System.Data.SQLite 1.0.96.0。


我不得不同时添加System.Data.SQLite - Ian McLaird
我有相同的问题,但我不理解您如何首先将该xml文件放入您的app.config中。我不需要任何东西,没有app.config,只需为SqlServer提供连接字符串即可运行EF查询正常。那么,您是如何将所有“DbProviderFactories”等内容放入配置中的?我需要它吗?如果需要,我怎样才能在不从错误的位置复制旧配置的情况下将其放入其中? - PandaWood
7个回答

102

我只需在 App.config 中添加一行代码,就解决了相同的错误。

<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>

提示: 在 <configuration> > <entityFramework> > <providers> 中添加 provider


答案似乎与以下内容相关:https://dev59.com/bV8d5IYBdhLWcg3w41gp - dsum
7
已为我解决了,谢谢。不知道为什么它没有自动生成。 - Dan
会支持 @Dan 的意见 - Lakshman Pilaka

13

这里是一个可用的app.config文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>

  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
    </providers>
  </entityFramework>

  <connectionStrings>
    <!-- use AppDomain.SetData to set the DataDirectory -->
    <add name="MapDbConnectionStr" connectionString="Data Source=|DataDirectory|MapDb.sqlite" providerName="System.Data.SQLite" />
  </connectionStrings>

  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>



</configuration>

1
不知道为什么这么难找。英雄。 - BennyM
1
这是我在尝试了许多不同的解决方案后唯一有效的方法! - Ihab

6

我终于让它运行起来了。

我使用的是: EF 6.1.3 http://www.microsoft.com/en-us/download/details.aspx?id=40762 和 System.Data.SQLite 1.0.96.0 sqlite-netFx451-setup-bundle-x86-2013-1.0.96.0.exe

我按照这篇文章中的描述进行操作: Database first create entity framework 6.1.1 model using system.data.sqlite 1.0.93 (在这个描述中,一个Entity Framework的nuget包被安装了 - 我也这样做了)

对于app.config文件,我使用了以下的修复方法: https://dev59.com/Y2Uq5IYBdhLWcg3waP3r#24324212 (由tomexou编写)

最后,在ADO.Net Entity Data Model Mapper中没有显示SQLite Connector。

缺少的连接是\bin文件夹。 我必须为以下dll设置“Copy Local”= true设置:

  • SQLite.Designer
  • System.Data.SQLite
  • System.Data.SQLite.EF6
  • System.Data.SQLite.Linq

只是为了完整性 - 通过Nuget添加并放在\bin文件夹中

  • EntityFramework
  • EntityFramework.SqlServer

然后SQLite连接就被显示了...


3

试试这些调整:

<providers>
  ...
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
</providers>

<system.data>
    <DbProviderFactories>
        <remove invariant="System.Data.SQLite.EF6" />
        <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
</system.data>

请查看Entity Framework 6 + SQLite

我仍然看到相同的错误。只是为了澄清,我应该在<providers>部分中保留SQLite的其他条目,对吗?因此,有一个用于System.Data.SQLite.EF6和System.Data.SQLite。无论哪种方式,它都不起作用。我能够像以前一样查看/创建数据连接,但在执行时失败。 - Simon
我的问题出现在更新后,所以我按照链接中所示更改了我的配置。你的连接字符串是什么样子的? - Steve Greene
元数据=res:///DbModel.csdl|res:///DbModel.ssdl|res://*/DbModel.msl;提供程序=System.Data.SQLite.EF6;提供程序连接字符串="data source=C:\sqlite_test\testDb" - Simon
我的提供程序只是System.Data.SQLite;不确定这是否有影响。 - Steve Greene
如果我从<providers>中删除System.Data.SQLite不变量,那么在生成数据库向导数据源窗口中将不再显示SQLite作为数据源。对于这个问题,我有点茫然。 - Simon

3

感谢您的回复。我最终成功让它运行得足够好(虽然没有使用一些VS设计工具)以完成我需要的工作,但希望在未来的版本中设置会更容易些。 - Simon
我可以问一下你是怎么解决问题的吗?也许能帮助我解决我的问题 :) - ImP
说实话,那是几周前的事情,我折腾了很久。我决定采用数据库优先的方法,因为这是一个相对简单的应用程序,而且确实起作用了。我会在上面编辑中发布我的App.config属性。希望能有所帮助!祝好运! - Simon

-1

对我来说,没有任何建议的解决方案起作用。无论是这里的还是答案中的linked related thread,当我尝试运行Add-Migration SampleMigrationUpdate-Database时都不行。

如果这可以帮助其他遇到同样问题的人,我想分享我们当前的解决方案。 我对EF不是很了解,但是我的同事找到了这个解决方法,使VS能够识别类型并将其包含在构建管道中。

// this is required for migrations to work, VS complains about missing sql providers
var x = typeof(System.Data.Entity.SqlServer.SqlProviderServices);

-2
public class EFConfiguration : DbConfiguration
{
    public EFConfiguration()
    {
        SetDefaultConnectionFactory(new LocalDbConnectionFactory("v.11"));

        //HACK
        var EF6ProviderServicesType = typeof(System.Data.SQLite.EF6.SQLiteProviderFactory).Assembly.DefinedTypes.First(x => x.Name == "SQLiteProviderServices");
        var EF6ProviderServices = (DbProviderServices)Activator.CreateInstance(EF6ProviderServicesType);
        SetProviderServices("System.Data.SQLite.EF6", EF6ProviderServices);
        SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
        SetProviderFactory("System.Data.SQLite.EF6", System.Data.SQLite.EF6.SQLiteProviderFactory.Instance);
        SetProviderFactory("System.Data.SQLite", System.Data.SQLite.SQLiteFactory.Instance);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接