简体   繁体   English

在Sqlite中使用实体框架

[英]Using Entity-Framework with Sqlite

I'm trying to use Sqlite with my C# Console App but when trying to add the Entity-Framwork data model I get the following message: 我试图在我的C# Console App使用Sqlite ,但是当尝试添加Entity-Framwork data model ,出现以下消息:

Your project references the latest version of Entity Framework; 您的项目引用了Entity Framework的最新版本; however, an Entity Framework database provider compatible with this version could not be found for your data connection. 但是,找不到与该版本兼容的Entity Framework数据库提供程序作为您的数据连接。 If you have already installed a compatible provider, ensure you have rebuilt your project before performing this action. 如果已经安装了兼容的提供程序,请确保在执行此操作之前已重建项目。 Otherwise, exit the wizard, install a compatible provider, and rebuild your project before performing this action. 否则,请退出向导,安装兼容的提供程序,然后重新生成项目,然后再执行此操作。

Steps to reproduce: 重现步骤:

  • Add a new .NET 4 Console Application 添加一个新的.NET 4 Console Application
  • Install the System.DataBase.SQLite (x86/x64) Nuget package (which automatically installs the Entity-Framwork package as a dependency) 安装System.DataBase.SQLite (x86/x64) Nuget package (它将自动将Entity-Framwork程序包作为依赖项安装)
  • Add an ADO.NET Entity Data Model with the Add new item dialog 使用“添加新项”对话框添加ADO.NET Entity Data Model

after following the above steps the following app.config was created: 完成上述步骤后,将创建以下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>

and the following packages.config : 和以下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>

You only need these packages: 您只需要这些软件包:

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

And then set you build configuration to x86! 然后将您的构建配置设置为x86!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM