title: How to make EF Core Include() not keep track of entities?
body: <p><strong>TLDR</strong>: I would like to know if using different "include logics" for one entity type in a single query possible in EF core.</p>
  
  <p><strong>Edit</strong>: Just to be clear, in the title I said keeping track of entities because I think that's what EF does, but <code>.AsNoTracking()</code> does nothing useful here, before anyone suggests.</p>
  
  <p>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 <code>api/parents</code>, I want the application to give me a json that looks like this:</p>
  
  <pre><code>[
    {
      "id": "parentid1",
      "extraProperty": "value",
      "children": [
        {
          "id": "childid1",
          "parent": {
            "id": "parentid1",
            "extraProperty": "value"
          }
        }
      ]
    }
  ]
  </code></pre>
  
  <p>My setup looks like this:</p>
  
  <p><strong>EF query</strong>:</p>
  
  <pre><code>(from p in _context.Parents
   select p)
   .Include(p =&gt; p.Children)
       .ThenInclude(c =&gt; c.Parent)
   .ToList();
  </code></pre>
  
  <p>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 <code>SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore</code>.</p>
  
  <p>With this setup the api call gives back this response:</p>
  
  <pre><code>[
    {
      "id": "parentid1",
      "extraProperty": "value",
      "children": [
        {
          "id": "childid1"
        }
      ]
    }
  ]
  </code></pre>
  
  <p>Which as you can see "ignores" the self reference of the parent.</p>
  
  <p>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:</p>
  
  <pre><code>[
    {
      "id": "parentid1",
      "extraProperty": "value",
      "children": [
        {
          "id": "childid1",
          "parent": {
            "id": "parentid1",
            "extraProperty": "value",
            "children": [
              {
                "id": "childid1",
                "parent": {
                  "id": "parentid1",
                  "extraProperty": "value"
                  ...
                }
              }
            ]
          }
        }
      ]
    }
  ]
  </code></pre>
  
  <p>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.</p>
  
  <p>To my best guess, EF uses the initial setup <code>.Include(child).ThenInclude(parent)</code> when it encounters with any Parent object for that query. So when it finds a <code>Parent</code> object in the <code>Child</code> object, instead of using no includes (which I don't have any after the <code>ThenInclude</code>) it uses <code>.Include(child).ThenInclude(parent)</code>.</p>
  
  <p>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.</p>