Environment:
VS2017 Community
Windows 10
Postgresql 9.6
To install PostgreSQL:
https://www.postgresql.org/download/
1. Install the NuGet Packages we need:
3.1. 1 Install 3.1. 1
2. First install a VS extension tool so that we can connect to the PostgreSQL database in Server Explorer just like in SQL.
3. After installing the above extension, we will build our connection in SE.
4. We chose the database first method, so we then created our Datamodel from the db.
5. After creating the model, we try to create an EF control.
6. See the following error.
7.Build after our project, we see this error.
8. Add the following configuration to the 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. You should be able to see that Conttroller was created successfully.
10. However, when visiting, it is possible to form a dead loop due to the relationship between our table index and foreign key, we need to add the following configuration in Global.asax.
httpconfiguration config = globalconfiguration.configuration;config. Formatters.jsonformatter . Serializersettings = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
You can then use Postman to test your API for what you want to return.
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
Connect to PostgreSQL using EF