实体框架6 + SQLite

nmu*_*kin 11 sqlite ado.net entity-framework entity-framework-6

我正在尝试使用EF6 alpha和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>
Run Code Online (Sandbox Code Playgroud)

我跑的时候

using (var dbContext = new TestDbContext())
{
    if (dbContext.Database.Exists())
    {
        dbContext.Database.Delete();
    }
    dbContext.Database.Create();
}
Run Code Online (Sandbox Code Playgroud)

我收到一个错误:

System.InvalidOperationException:System.InvalidOperationException:实体框架提供程序类型'System.Data.SQLite.SQLiteFactory,System.Data.SQLite,Version = 1.0.66.0,Culture = neutral,PublicKeyToken = db937bc2d44ff139'的'Instance'成员没有返回一个继承自'System.Data.Entity.Core.Common.DbProviderServices'的对象.实体框架提供者必须从此类扩展,并且"实例"成员必须返回提供者的Singleton实例.

我究竟做错了什么?

tom*_*xou 19

如果您使用的是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>
Run Code Online (Sandbox Code Playgroud)

这个对我有用.


bri*_*lam 16

需要更新System.Data.SQLite实体框架提供程序以使用实体框架的第6版.(请参阅为EF6重建EF提供程序)

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

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

2013年6月21日更新: 我在博客上分享了提供商的更新版本.有关更多信息,请参阅Entity Framework 6上的System.Data.SQLite.


zch*_*pit 6

我在我的Bitbucket帐户上安装了带有Sqlite的EF 6.0的工作解决方案:https://zchpit@bitbucket.org/zchpit/sqlitesamples.git

或者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>
Run Code Online (Sandbox Code Playgroud)