TLDR: I would like to know if using different "include logics" for one entity type in a single query possible in EF core.
Edit: Just to be clear, in the title I said keeping track of entities because I think that's what EF does, but .AsNoTracking()
does nothing useful here, before anyone suggests.
The problem at hand is on a relatively small React application backed with an ASP.NET Core web api application. What I want to accomplish is, when I call api/parents
, I want the application to give me a json that looks like this:
[
{
"id": "parentid1",
"extraProperty": "value",
"children": [
{
"id": "childid1",
"parent": {
"id": "parentid1",
"extraProperty": "value"
}
}
]
}
]
My setup looks like this:
EF query:
(from p in _context.Parents
select p)
.Include(p => p.Children)
.ThenInclude(c => c.Parent)
.ToList();
After that I have AutoMapper mapping the entity to the dto, there is not much going on there. I also use the default Json Serializer (Newtonsoft) for the application. It is configured with SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore
.
With this setup the api call gives back this response:
[
{
"id": "parentid1",
"extraProperty": "value",
"children": [
{
"id": "childid1"
}
]
}
]
Which as you can see "ignores" the self reference of the parent.
The solution I came up with was, I should configure Newtonsoft to "serialize" reference loops, and I tried it. And it throws, because the EF query provided above returns an entity list that looks like this:
[
{
"id": "parentid1",
"extraProperty": "value",
"children": [
{
"id": "childid1",
"parent": {
"id": "parentid1",
"extraProperty": "value",
"children": [
{
"id": "childid1",
"parent": {
"id": "parentid1",
"extraProperty": "value"
...
}
}
]
}
}
]
}
]
If you look at my Include calls, it explicitly says that for my child objects I only want the parent object and no other references inside that parent object.
To my best guess, EF uses the initial setup .Include(child).ThenInclude(parent)
when it encounters with any Parent object for that query. So when it finds a Parent
object in the Child
object, instead of using no includes (which I don't have any after the ThenInclude
) it uses .Include(child).ThenInclude(parent)
.
I don't want to solve this problem through hacks with either the mapper or the serializer if I'm not have to. I would like to know if what I am seeking is possible: To use different "include logics" for one entity type in a single query.
child.Parent
object is the sameparent
reference wherechild
is inparent.Children
collection.