Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading Related Data with Include() #14147

Closed
wangkanai opened this issue Dec 12, 2018 · 4 comments
Closed

Loading Related Data with Include() #14147

wangkanai opened this issue Dec 12, 2018 · 4 comments

Comments

@wangkanai
Copy link

wangkanai commented Dec 12, 2018

I trying to get related data loaded with this api method

// GET: api/Department
[HttpGet]
public async Task<ActionResult<IEnumerable<Department>>> GetDepartment()
{
    var result = await _context.Departments
        .Include(d =>d.Children)
        .Where(d => d.Parent == null)                
        .ToListAsync();
    return result;
}

But i just end up with follow result Code 200

can't parse JSON.  Raw result:

[{"id":1,"parentId":null,"parent":null,"children":[{"id":2,"parentId":1

using the following model

public class Department
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int? ParentId { get; set; }
    public Department Parent { get; set; }
    public ICollection<Department> Children { get; set; }

    public int SiteId { get; set; }
    public Site Site { get; set; }

    public int DepartmentTypeId { get; set; }
    public DepartmentType DepartmentType { get; set; }
    [Required]
    public string Title { get; set; }
    public string Description { get; set; }
}

The full commit could be found here https://github.com/wangkanai/Organization/commit/683398c6e909cd3dc7049afc3ede66bfcf75c470
Its not that i never got this parent child relation working, i have a working model at https://github.com/wangkanai/EntityPattern/tree/master/ParentChild

Not sure what is going on, I run debug and found that data is getting load to var result.
image

Its like that IEnumerable<Department> is NOT parsing to ActionResult() or something.

Platform:

  • ASP.NET Core 2.2
  • EF Core 2.2
@smitpatel
Copy link
Contributor

Did you configure your API to ignore loops?

@smitpatel
Copy link
Contributor

Duplicate of aspnet/Mvc#4160

@wangkanai
Copy link
Author

I will continue with this issue because I could not add more comments to aspnet/Mvc#4160

Well I have trying adding ReferenceLoopHandling.Ignore to Startup.cs
image

Look at this reference loop issue is still present.
image

But if add [JsonIgnore] the Api doesn't produce error, while children doesn't get loaded to the result.
This because the object is not get serialize to ActionResult.
image

Therefore, its seem like ASP.NET 2.2 & EFCore 2.2 doesn't respect this ReferenceLoopHandling.Ignore fix anymore.

Reference https://github.com/wangkanai/Organization/commit/9ca3b16752409cd153a33260cc39cbeb0c8e9149

@smitpatel
Copy link
Contributor

@wangkanai - ReferenceLoopHandling is not something EF Core processes. It configures the Json serialization. EF just creates result on client side. Seems like you are having issues in your serialization code path rather than EF. Given ignoring loop in results works for people using serialization, try asking question on stackoverflow.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants