将实体框架与 Sqlite 结合使用

本文关键字:结合 Sqlite 实体 框架 | 更新日期: 2023-09-27 18:30:34

我正在尝试将Sqlite与我的C# Console App一起使用,但是当尝试添加Entity-Framwork data model时,我收到以下消息:

您的项目引用最新版本的实体框架;但是,找不到与此版本兼容的数据连接的实体框架数据库提供程序。如果已安装兼容的提供程序,请确保在执行此操作之前已重新生成项目。否则,请退出向导,安装兼容的提供程序,然后重新生成项目,然后再执行此操作。

重现步骤:

  • 添加新.NET 4 Console Application
  • 安装
  • System.DataBase.SQLite (x86/x64) Nuget package(自动将Entity-Framwork包安装为依赖项)
  • 使用"添加新项"对话框添加ADO.NET Entity Data Model

执行上述步骤后,创建了以下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>
  <system.data>
    <!--
        NOTE: The extra "remove" element below is to prevent the design-time
              support components within EF6 from selecting the legacy ADO.NET
              provider for SQLite (i.e. the one without any EF6 support).  It
              appears to only consider the first ADO.NET provider in the list
              within the resulting "app.config" or "web.config" file.
    -->
    <DbProviderFactories>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".NET Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
      <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" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <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" />
    </providers>
  </entityFramework>
</configuration>

以及以下packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.1" targetFramework="net40" />
  <package id="System.Data.SQLite" version="1.0.94.1" targetFramework="net40" />
  <package id="System.Data.SQLite.Core" version="1.0.94.0" targetFramework="net40" />
  <package id="System.Data.SQLite.EF6" version="1.0.94.0" targetFramework="net40" />
  <package id="System.Data.SQLite.Linq" version="1.0.94.1" targetFramework="net40" />
</packages>

将实体框架与 Sqlite 结合使用

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  <providers>
    <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    <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" />-->
  </providers>
</entityFramework>
<connectionStrings>
  <add name="SettingContext" connectionString="Data Source=setting.db" providerName="System.Data.SQLite" />

<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" />
    <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" />
</DbProviderFactories>
</system.data>

您只需要以下软件包:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.1.1" targetFramework="net40" />
  <package id="System.Data.SQLite.Core" version="1.0.94.0" targetFramework="net40" />
  <package id="System.Data.SQLite.EF6" version="1.0.94.0" targetFramework="net40" />
</packages>

然后将构建配置设置为 x86!