簡體   English   中英

在Sqlite中使用實體框架

[英]Using Entity-Framework with Sqlite

我試圖在我的C# Console App使用Sqlite ,但是當嘗試添加Entity-Framwork data model ,出現以下消息:

您的項目引用了Entity Framework的最新版本; 但是,找不到與該版本兼容的Entity Framework數據庫提供程序作為您的數據連接。 如果已經安裝了兼容的提供程序,請確保在執行此操作之前已重建項目。 否則,請退出向導,安裝兼容的提供程序,然后重新生成項目,然后再執行此操作。

重現步驟:

  • 添加一個新的.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>

您只需要這些軟件包:

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

<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