title: Entity Framework Core returning object with many to many relationship
body: <p>I'm not sure how to get the information I need using EF Core with a .NET Core app. I have two database tables - OBJECTS and TAGS with a many-to-many relationship (using a third join table OBJECTTAGS). I want to get all the objects (about 1400) along with their associated tags.</p>
  
  <p>These are my three models:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace ContentMarketplace.Models
  {
      public partial class Object
      {
          public int ObjectId { get; set; }
          ...
          public virtual List&lt;ObjectTag&gt; ObjectTags { get; set; }
      }
  }
  
  namespace ContentMarketplace.Models
  {
      public partial class Tag
      {
          public int TagId { get; set; }
          ...
          public virtual List&lt;ObjectTag&gt; ObjectTags { get; set; }
      }
  }
  
  namespace ContentMarketplace.Models
  {
      public partial class ObjectTag
      {
          public int ObjectId { get; set; }
          public virtual Object Object { get; set; }
          public int TagId { get; set; }
          public virtual Tag Tag { get; set; }
      }
  }
  </code></pre>
  
  <p>And this is what's in the OnModelCreating() method in my context:</p>
  
  <pre><code>modelBuilder.Entity&lt;ObjectTag&gt;(entity =&gt;
  {
      entity.HasKey(e =&gt; new { e.ObjectId, e.TagId });
  
      entity.HasOne(ot =&gt; ot.Object)
          .WithMany(o =&gt; o.ObjectTags)
          .HasForeignKey(ot =&gt; ot.ObjectId);
  
      entity.HasOne(ot =&gt; ot.Tag)
          .WithMany(t =&gt; t.ObjectTags)
          .HasForeignKey(ot =&gt; ot.TagId);
  });
  </code></pre>
  
  <p>This issue arises when I try to return data in my ObjectController.cs</p>
  
  <pre><code>namespace ContentMarketplace.Controllers
  {
      [Route("api/[controller]")]
      [ApiController]
      public class ObjectController : ControllerBase
      {
          private readonly ContentMarketplaceContext _context;
  
          public ObjectController(ContentMarketplaceContext context)
          {
              _context = context;
          }       
  
          [HttpGet]
          public ActionResult&lt;List&lt;ContentMarketplace.Models.Object&gt;&gt; GetAll()
          {
              return _context.Objects
                  .Include(o =&gt; o.ObjectTags)
                  .ThenInclude(ot =&gt; ot.Tag)
                  .ToList();
          }
      }
  }
  </code></pre>
  
  <p>The Include().ThenInclude() creates a circular relationship where an HTTP request doesn't just return the tag information associated with each object but then all the objects associated with each of those tags and so on, crashing the browser.</p>
  
  <p>If I take out the ThenInclude() it works fine but doesn't return all the tag info not in my ObjectTag model.</p>
  
  <p>I know this has to do with EF Core auto loading stuff that's already in the context (like the "tip" notes here <a href="https://docs.microsoft.com/en-us/ef/core/querying/related-data" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/querying/related-data</a>) but I don't know how else to return JUST the object and tags without going any further. </p>