我目前正在構建一個圍繞EF4連接到MSSQL的應用程序。 現在已經決定,我們也應該能夠連接到SQLite。Entitiy框架:單個模型上的單個應用程序中的多個數據庫提供程序
我已經升級了應用程序到EF6,確保一切按照MSSQL的預期工作,現在我試圖讓應用程序連接到SQLite數據庫(這是我們的mssql數據庫的轉換版本)。
不幸的是我有點碰壁了。
如果我嘗試創建SQLite的方面,我們爲MSSQL
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = "System.Data.SQLite.EF6";
entityBuilder.ProviderConnectionString = "data source=sqlite_master.db";
entityBuilder.Metadata = @"res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl";
connection = new EntityConnection(entityBuilder.ToString());
connection.Open();
theEntities = new masterEntities(connection); // masterEntities extends System.Data.Entity.DbContext
getDataBaseVersion();
做同樣的方式,我得到一個異常指出「System.Data.SQLite.SQLiteConnection」不能被轉換爲「System.Data。 SqlClient.SqlConnection「中的getDataBaseVersion方法(這是第一個對實體運行查詢)。
我已經嘗試了一些東西,但總是得到這個或「意外的代碼第一個異常」。
Btw:我並不需要在運行時爲SQLite版本創建連接字符串。如果問題可以通過使用配置文件解決,我會很樂意做到這一點。
我的app.config看起來是這樣的:
<entityFramework>
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<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" />
</DbProviderFactories>
</system.data>
我的猜測是,實體數據模型內的某處data.sqlclient類設置爲使用。但是我不能創建第二個實體數據模型,因爲所有的類和屬性都會被定義兩次。 (我也不能,因爲sqlite的數據源不會顯示在實體數據模型中,但我希望能夠通過重新安裝所有東西來修復)
現在我想知道:是否可以在同一個應用程序中爲同一個模型使用兩個不同的提供者? 如果是的話,如果有人能指出我解決問題的方法,我會非常高興。
更新 我想我已經取得了一些進展,昨晚:在App.config加入.EF6每System.Data.SQLite後,我現在得到一個不同的異常。
的應用程序配置現在看起來是這樣的:
<entityFramework>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</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" />
</DbProviderFactories>
</system.data>
我現在得到一個異常,說明了providerfactory提供者的名稱「System.Data.SQLite.SQLiteFactory」無法找到。
我還沒有看過它,會在一天中這樣做。
CNC中 我
EntityFramework.dll // from nuget
EntityFramework.BulkInsert // from nuget
EntityFramework.MappingAPI // from nuget
EntityFramework.SqlServer // from nuget
System.Data
System.Data.DataSetExtensions
System.Data.Linq
System.Data.SQLite // from the sqlite-netFx451-setup-bundle-x86
System.Data.SQLite.EF6 // from the sqlite-netFx451-setup-bundle-x86
我真的不能在那裏看到問題,我懷疑你的引用程序集或你的上下文構造函數有錯誤。爲什麼不使用DbContext(Connection,contextOwnsConnection)重載?我發現這比在app.cfg中定位連接字符串要容易得多。 – DevilSuichiro
感謝您的回覆。我在幕後使用了DbContext(Connection,contextOwnsConnection)。被調用的masterEntities的構造函數如下所示: public masterEntities(System.Data.Common.DbConnection connection) :base(connection,true) { } – user5383875
該問題可能位於EntityConnection中(必須是直到現在,它已經轉換爲SQLConnection,現在它必須被轉換爲SQLiteConnection)。你明確地施展這些關係嗎?無論如何,我注意到你讀了System.Data.Sqlite.SqliteProviderServices(沒有EF)。它是否正確?我真的不知道我是如何自己處理這個問題的,不得不在明天工作,並且不應該引起這個問題,但是這是需要注意的。 – DevilSuichiro