环境:html
VS2017 Communitygit
Windows 10github
Postgresql 9.6 sql
安装Postgresql:数据库
https://www.postgresql.org/download/ide
1.安装咱们须要的Nuget Packages:工具
install-package npgsql -version 3.1.1 Install-Package EntityFramework6.Npgsql -Version 3.1.1
2.首先得安装一个vs的扩展工具,这样咱们才能在server explorer中像连sql中同样链接到postgresql数据库。oop
3. 安装好上面的扩展后,咱们就在se中创建好咱们的链接。post
4.咱们选择的是Database First的方式,因此以后咱们是从DB里面来建立咱们的DataModel。测试
5.创建好Model后,咱们试着来建立一个EF的Control.
6.看到如下这个错误。
7.Build一下咱们的project以后,接着看到这个错误。
8.在Web config 里加入以下的配置。
<system.data> <DbProviderFactories> <remove invariant="Npgsql"/> <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" support="FF"/> </DbProviderFactories> </system.data>
9.应该能看到conttroller建立成功了。
10.可是访问的时候,可能因为咱们的表里索引和外键的关系造成死循环,咱们须要在Global.asax中加入以下的配置。
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter
.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
接下来能够使用postman来测试本身的API是否是本身想要返回的东西了。
References:
http://www.npgsql.org/doc/index.html
https://www.nuget.org/packages/EntityFramework6.Npgsql/
https://www.nuget.org/packages/Npgsql/
https://github.com/npgsql/npgsql/issues/1439
https://wiki.postgresql.org/wiki/Using_Microsoft_.NET_with_the_PostgreSQL_Database_Server_via_ODBC
https://stackoverflow.com/questions/19467673/entity-framework-self-referencing-loop-detected