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

你可能感兴趣的:(PostgreSQL)