最近做了一個小應用,使用SQLite做資料庫。開始用DBLINQ的時候,做一個LINQ查詢出現不支援的問題。後來看到Entity Framework是可以支援SQLite的,於是很快轉換過來。完成開發,在開發機器上測試正常。部署到正式環境中,開始出現“指定的儲存區提供者在配置中找不到,或者無效。”----> "找不請求的.Net Framework資料提供者。可能沒有安裝”。
這個問題出現的很奇怪,因為生產機器上也運行了SQlite.net 的安裝包。
運行了一個測試程式,輸出 DbProviderFactories.GetFactoryClasses()的DataTable,發現生產環境的Data Provider的確沒有SQLite這一項。
在網上查了一圈,發現有人提示:
1. Add the following to the Web.config file:
<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.57.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
/>
</DbProviderFactories>
</system.data>
2. Copy System.Data.SQLite.DLL AND System.Data.SQLite.Linq.dll files to the Bin directory
根據1 的提示,我把
<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.57.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139"
/>這段配置,也加到生產環境的Machine.config裡面,再運行測試程式,SQLite項出現在DbProviderFactories.GetFactoryClasses()的DataTable了,說明配置沒有問題了。
但是運行程式,還是報錯:“對類型"System.Data.SQLite.SQLiteFactory"的儲存區提供者調用"GetService"方法後,返回null。儲存區提供者可能未正常運行”。
花了一段時間研究“儲存區”,無功而返。
突然發現生產環境的GAC沒有System.Data.SQLite,問題是不是在這裡呢?
在生產環境下v1.1.xxx下的GacUtil.exe運行之後,報錯unknown。又在網上查了一下,據說是GacUtil和Framework的版本有關係。在開發機器上C:/Program Files/Microsoft SDKs/Windows/v6.0A/Bin找到一個GacUtil.exe,一看,的確這個是3.5的版本。拷貝到生產環境,註冊System.Data.SQLite.DLL和System.Data.SQLite.Linq.DLL,運行程式:一切正常!大功告成!
最後總結問題所在,有兩點:
1 GAC沒有註冊 System.Data.SQLite.DLL和System.Data.SQLite.Linq.DLL
2 Machine.config的DBProviderFactories沒有正確增加SQLite相關項目。