標籤:
Entity Framework 6串連Postgresql、SQLite、LocalDB的注意事項和設定檔 - 玄離時間 2014-09-03 01:05:00 部落格園精華區原文 http://www.cnblogs.com/znlgis/p/3952673.html
Postgresql
Postgresql支援Code First的方式自動產生表,不過預設的模式是dbo而不是public,而且還可以自動產生自增主鍵。
<?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> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="Npgsql"></remove> <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql" /> </DbProviderFactories> </system.data> <connectionStrings> <add name ="BrKxxContext" connectionString ="server=127.0.0.1;User Id=postgres;password=energy;database=eftest" providerName="Npgsql"/> </connectionStrings> </configuration>
SQLite
SQLite不支援Code First的方式自動產生表,所以可能會報找不到相應的table的錯誤,沒辦法只能自己手動建表了,另外,SQLite雖然支援INTEGER類型的自增主鍵,但是INTEGER在C#中對應的是long或者Int64的類型,這個是要注意的,給Model設定主鍵時一定要設對類型,不然會出問題。
<?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> <connectionStrings> <add name="BrKxxContext" providerName="System.Data.SQLite.EF6" connectionString="Data Source=|DataDirectory|BrKxx.db;Pooling=True" /> </connectionStrings> <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> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </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" />--> <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" /> </providers> </entityFramework> </configuration>
LocalDB
1、如果是單獨只安裝LocalDB的情況下,LocalDB不會自動開啟預設的進程,需要通過提供的SqlLocalDB.exe來建立或者開啟相應的進程
2、如果部署的機器上只安裝了.NET 4.0的運行環境,那麼你的應用程式將無法訪問LocalDB,你需要安裝.NET 4.0.2以上的版本才能正常的訪問LocalDB
<?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> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0"/> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/> </providers> </entityFramework> <connectionStrings> <add name="BrKxxContext" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=BrKxx;AttachDbFilename=|DataDirectory|BrKxx.mdf;Integrated Security=True;"/> </connectionStrings> </configuration>
最後
DataDirectory一般是指App_Data,如果使用預設值得情況下,提示找不到資料庫檔案,或者你想把資料庫檔案放在指定的路徑下,就需要在程式啟動的地方人為的設定DataDirectory
AppDomain.CurrentDomain.SetData("DataDirectory", Application.StartupPath + "\\App_Data\\");
.NET的EF+POSTGRESQL事項。