天天看點

在VS2013環境下使用EF架構與Sqlite(一)

在VS開發+EF架構的環境下使用Sqlite資料庫是一個比較繁瑣的事情, 因為Sqlite針對不同版本的VS, 有着不同版本的vs環境安裝插件,如果不太懂的話,很容易裝錯版本導緻在建立Model的時候無法看到資料庫連接配接.在此就将具體的連接配接步驟在這裡詳細說明一下.

一、下載下傳并安裝SQLite的VS插件

登入網站 https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki ,下載下傳屬于你自己IDE版本的Sqlite 的VS插件.這裡需要注意兩點: 一是 清單裡秘密麻麻給了好多插件下載下傳包,實際上, 隻有下載下傳描述帶有“32-bit”(不管你的實際電腦環境是64還是32都要選這個),檔案名帶“ bundle”,才是能給VS安裝SQLite插件的, 第二點就是安裝包一定要符合自己的FrameWork版本. 

比如我自己的windows版本是64位,開發環境是VS2013, framework版本是 4.5 , 那麼我需要的插件版本就是:

在VS2013環境下使用EF架構與Sqlite(一)
在VS2013環境下使用EF架構與Sqlite(一)
在VS2013環境下使用EF架構與Sqlite(一)

直接下載下傳,點選下一步安裝即可.

需要注意的是,一定要在這個網站上進行最新插件的下載下傳, 其他部落格給出的下載下傳包有的都是過時的了,安上了可能會不好用.這點一定要注意.

二、項目代碼裡引用EF和SQLite的dll檔案

右鍵項目,點選"管理Nuget"程式包,将下圖的前五個安裝包搜尋并下載下傳即可(我沒特意下載下傳過第六個,我也不知道它是怎麼來的)

在VS2013環境下使用EF架構與Sqlite(一)
在VS2013環境下使用EF架構與Sqlite(一)

三、對App.Config檔案進行更改(敲三遍小黑闆,這裡很重要!!!)

如果将上面步驟都搞定,理論上已經把EF和SQLite的相關環境搭建完畢.但實際上還有個地方需要做更改,否則EF還是無法正确執行,會報出如下的錯誤:

Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
           

 大概是說沒有找個支援ado.net的管道工廠方法。在stackoverflow搜尋發現,需要在Web.config中添加對System.Data.SQLite.SQLiteFactory的配置。

其實原因很簡單: 在Nuget下載下傳完上面5個安裝包後, 會自動改寫程式所在的App.Config檔案. 但有個兩個點該寫的不符合EF環境的引用情況.是以需要我們手工再增加一些配置.

打開App.Config檔案,按照如下節點進行查找

configuration→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" />
      
           

這兩行配置其實是錯誤的,并沒有真正引用到我們需要的dll, 是以我們放着這兩行不去管它,再增加如下兩行記錄

<!--這兩個屬性是需要後添加的,否則EF會報錯 -->
      <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" />
      
           

整個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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <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" />
    </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" />
      <!--這兩個屬性是需要後添加的,否則EF會報錯 -->
      <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>
  <connectionStrings>

    <add name="testEntities1" connectionString="metadata=res://*/LocalModel.csdl|res://*/LocalModel.ssdl|res://*/LocalModel.msl;provider=System.Data.SQLite.EF6;provider connection string="data source=C:\Users\Administrator\Desktop\下載下傳目錄\SQLiteSpy_1.9.8\test.db"" providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
           

這樣的,整個EF和SQLite的架構所需的基礎引用方面就搞定了, 下一篇文章将介紹如何進行EF項目的建立.

自己解決問題的時候參考了 李飛麟的部落格 ,特表感謝!