实体框架 6 + SQLite

我正在尝试使用 EF6 alpha 和 SQLite 1.0.66.0

I'm trying to use EF6 alpha and SQLite 1.0.66.0

我的 .config 文件:

My .config file:

<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,文化=中性,PublicKeyToken=db937bc2d44ff139'没有返回继承自的对象'System.Data.Entity.Core.Common.DbProviderServices'.实体框架提供者必须从此类扩展,并且实例"成员必须返回提供者的单例实例..

System.InvalidOperationException: System.InvalidOperationException: The 'Instance' member of the Entity Framework provider type 'System.Data.SQLite.SQLiteFactory, System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must extend from this class and the 'Instance' member must return the Singleton instance of the provider..

我做错了什么?

推荐答案

如果您使用的是 EF 6.1.3 + System.Data.SQLite v1.0.96.0,只需在 web.config 中调整(添加)以下声明即可.(你可以通过一些文本比较工具找到不同之处,我已经给它们编号了).

if you used EF 6.1.3 + System.Data.SQLite v1.0.96.0, just adjust(add) the following declarations in the web.config. (you can find the difference with some text-compare tool, i have numbered them).

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

它对我有用.

相关文章