Efcore.pg: Include function not working with pgsql and .net core 2.1

Created on 20 Nov 2018  路  8Comments  路  Source: npgsql/efcore.pg

I try to add countries in my address object like this

public IActionResult  AllAddress()
{
    var AllAddresses = _context.Addresses.Include(a=>a.Country);
    var result = new
    {
        Addresses = AllAddresses
    };

   return Ok(result);
}

but it just loop and and give me

http://localhost:9807/api/Addresses/all net::ERR_CONNECTION_RESET 200 (OK)

kindly note if I remove include function and return only _context.Addresses it working fine without any issue ,also I check the database and I am sure every foreign key in address has it's primary key in country ,
kindly help

Most helpful comment

You should adjust your JSON serialization settings to ignore circular references, like so:

```c#

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(x =>
{
// or ReferenceLoopHandling.Ignore
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
x.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
})
```

I'm going to close this issue now, as this is not related to EF Core. Please feel free to continue posting if you have additional questions or issues related to EF Core or the Npgsql provider.

All 8 comments

Can you please reproduce your issue with a quick and simple console application, instead of using ASP.NET? This will allow us to isolate and understand things better.

I suspect this won't be reproducible outside of ASP.NET Core. I've seen errors like this before, usually when I've botched the scopes/disposal of services.

Before attempting to reproduce, I suggest that you take a look at your scopes and see if something is disposed before those entities are available to the client app

I suspect this won't be reproducible outside of ASP.NET Core. I've seen errors like this before, usually when I've botched the scopes/disposal of services.

Before attempting to reproduce, I suggest that you take a look at your scopes and see if something is disposed before those entities are available to the client app

I use postgres sql DB , I make db first,
I use this command to create my models entites,

dotnet ef dbcontext scaffold "Host=localhost;Database=mad;Username=postgres;Password=aaaa" 
Npgsql.EntityFrameworkCore.PostgreSQL -o Models

after I install the below package

Install-Package Npgsql.EntityFrameworkCore.PostgreSQL.Design
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Npgsql.EntityFrameworkCore.PostgreSQL -Version 2.1.2
Install-Package Microsoft.EntityFrameworkCore.Proxies -Version 2.1.4

Can you please reproduce your issue with a quick and simple console application, instead of using ASP.NET? This will allow us to isolate and understand things better.

could you please see this project
it's so simple I just create new core vue project and
add to it my database using this command
https://github.com/alrazyreturn/core_project

dotnet ef dbcontext scaffold "Host=localhost;Database=mad;Username=postgres;Password=aaaa"
Npgsql.EntityFrameworkCore.PostgreSQL -o Models

I just try to include counteries inside address
and I have this error
as I say if I return address only I have no issue

@alrazyreturn wrote:

after I install the below package

Install-Package Npgsql.EntityFrameworkCore.PostgreSQL.Design
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Npgsql.EntityFrameworkCore.PostgreSQL -Version 2.1.2
Install-Package Microsoft.EntityFrameworkCore.Proxies -Version 2.1.4

You do not need to add Npgsql.EntityFrameworkCore.PostgreSQL.Design. That package is no longer maintained.

@alrazyreturn wrote:

Can you please reproduce your issue with a quick and simple console application, instead of using ASP.NET? This will allow us to isolate and understand things better.

could you please see this project
it's so simple I just create new core vue project and
add to it my database using this command
https://github.com/alrazyreturn/core_project

That project is far too involved for us to evaluate. As @roji wrote, we would need a minimal reproduction of this issue in the form of a console application that _does not_ use ASP.NET Core.

Before going any further, try the following and please report back:

```c#
public IActionResult AllAddress()
{
var AllAddresses = _context.Addresses.Include(a=>a.Country);
var result = new
{
Addresses = AllAddresses.ToList() // enumerate here in-scope
};

return Ok(result);
}
```

thank you actully I got the error ,
actully if i use include function it return country and each countery return again a collection of addresses and every addrees return it's country and so on in infinite loop,
when I remove
c# public ICollection<Addresses> Addresses { get; set; }
from Countries Model
and remove relation ship from context file also it solve the issue,

the question is should I have to make this every time I upload the database ,or there is other solution ?
kindly help

You should adjust your JSON serialization settings to ignore circular references, like so:

```c#

services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(x =>
{
// or ReferenceLoopHandling.Ignore
x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
x.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
})
```

I'm going to close this issue now, as this is not related to EF Core. Please feel free to continue posting if you have additional questions or issues related to EF Core or the Npgsql provider.

Many thanks its working fine now

Was this page helpful?
0 / 5 - 0 ratings