Entity Framework 6連接Postgresql、SQLite、LocalDB的注意事項和配置文件


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\\");


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM