实体框架6 + SQLite的(Entity Framework 6 + SQLite)

2019-07-18 16:29发布

我试图用EF6阿尔法和SQLite 1.0.66.0

我的config文件:

<connectionStrings>
   <add connectionString="data source=:memory:;" name="TestDbContext" providerName="System.Data.SQLite" />
</connectionStrings>
<entityFramework>
   <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
   </providers>
</entityFramework>
<runtime>
   <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
     <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
     </dependentAssembly>
   </assemblyBinding>
</runtime>
<system.data>
  <DbProviderFactories>
     <remove invariant="System.Data.SQLite"/>
       <add name="SQLite Data Provider" invariant="System.Data.SQLite"
           description=".Net Framework Data Provider for SQLite"
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
  </DbProviderFactories>
</system.data>

当我运行

using (var dbContext = new TestDbContext())
{
    if (dbContext.Database.Exists())
    {
        dbContext.Database.Delete();
    }
    dbContext.Database.Create();
}

我得到一个错误:

System.InvalidOperationException信息:System.InvalidOperationException:实体框架提供程序类型的“实例”成员“System.Data.SQLite.SQLiteFactory,System.Data.SQLite,版本= 1.0.66.0,文化=中性公钥= db937bc2d44ff139”没返回从“System.Data.Entity.Core.Common.DbProviderServices”继承的对象。 实体框架供应商必须从这个类扩展和“实例”成员必须返回供应商的Singleton实例..

我究竟做错了什么?

Answer 1:

如果你使用的EF 6.1.3 + System.Data.SQLite v1.0.96.0,只需调整(增加)在web.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" />
        <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> 

        <!-- 1. Solves SQLite error of "Unable to find the requested .Net Framework Data Provider."-->
        <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />

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

        <!-- 2. Solves SQLite error of "Unable to find the requested .Net Framework Data Provider."-->
        <remove invariant="System.Data.SQLite"/>
        <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>
</system.data>

这个对我有用。



Answer 2:

该System.Data.SQLite实体框架提供商将需要更新的实体框架的第6版工作。 (参见重建EF提供商EF6 )

对于SQLite的,这是一个相当简单的任务:

  1. 下载并打开System.Data.SQLite.Linq项目
  2. 删除提及System.Data.Entity.dll
  3. 加入EntityFramework.dll 6版本的引用
  4. 更新破命名空间引用
  5. 重建提供商

2013年6月21日更新:我已经在我的博客共享提供商的更新版本。 见System.Data.SQLite对实体框架6获取更多信息。



Answer 3:

我把EF 6.0使用SQLite的工作液在我的帐户到位桶: HTTPS://zchpit@bitbucket.org/zchpit/sqlitesamples.git

或混帐https://github.com/zchpit/SQLiteSamples

您可以下载从git仓库的工作方案。 在我的解决方案,我连接到SQLite的是:

  • 数据读取器
  • Simple.Data
  • EF 6.0

PS我的App.config

<?xml version="1.0"?>
<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="true" />
  </configSections>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client" />
  </startup>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Data.SQLite" publicKeyToken="db937bc2d44ff139" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.96.0" newVersion="1.0.96.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <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>
  <connectionStrings>
    <add name="SqlLiteContext" connectionString="Data Source=|DataDirectory|MyDatabase.sqlite" providerName="System.Data.SQLite" />
  </connectionStrings>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
    </providers>
    <defaultConnectionFactory type="System.Data.SQLite.SQLiteFactory, EntityFramework">
      <parameters>
        <!---parameter value="v11.0" />-->
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>


Answer 4:

System.Data.SQLite 1.0.91.0已更新,以支持EF6。 非常感谢柯的EF SQLite的优秀教程和更新。 你需要的,如果你想让它与教程一起更新您的新变化的app.config。 我可以证实,这为我工作在VS 2010中:

<?xml version="1.0"?>
<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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v11.0"/>
  </parameters>
</defaultConnectionFactory>
<providers>
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
  <remove invariant="System.Data.SQLite" />
  <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.91.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="ChinookContext" connectionString="Data Source=|DataDirectory|Chinook_Sqlite_AutoIncrementPKs.sqlite" providerName="System.Data.SQLite"/>
</connectionStrings>
</configuration>


Answer 5:

当重新安装的NuGet包的异常消失(System.Data.SQLite版本1.0.94.1)与

更新包-reinstall System.Data.SQLite



文章来源: Entity Framework 6 + SQLite