Connect to PostgreSQL using EF

Source: Internet
Author: User
Tags postgresql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.