title: How not to get a circular dependency?
body: <p>I have classes in the UserApp and Friends database.
  Eventually I look for only the friends class to fill the user.</p>
  <p>I saw the solution to this answer and is to add the package <code>Microsoft.AspNetCore.Mvc.NewtonsoftJson.</code></p>
  <p>I'm using aspnet core 3.1.6</p>
  <p>This my return Json</p>
  <pre><code>[
      {
          &quot;userAppId&quot;: &quot;705cbdaf-86e9-4759-8f85-4fa6f3560726&quot;,
          &quot;friendUserId&quot;: null,
          &quot;name&quot;: &quot;friend1Henrique&quot;,
          &quot;phone&quot;: &quot;123456778&quot;,
          &quot;areaCode&quot;: &quot;31&quot;,
          &quot;countryCode&quot;: &quot;+55&quot;,
          &quot;email&quot;: &quot;emailnaoobrigatorio@email.com&quot;,
          &quot;active&quot;: true,
          &quot;userApp&quot;: {
              &quot;name&quot;: &quot;Henrique&quot;,
              &quot;doc&quot;: &quot;123456789101&quot;,
  
              &quot;friends&quot;: []
  
          },
          &quot;accompaniment&quot;: null,
          &quot;id&quot;: &quot;eb6d0b7e-41ca-47f8-0e49-08d830c1215d&quot;
      }
  ]
  </code></pre>
  <p>I have my userApp inserted in it (JSON) and of course my friends class also inserted in userApp (in this JSON), because friends is child table. And this response caused this circular dependency.</p>
  <p>So, how can I not have this circular dependency? Or, what am I doing wrong since I would like to bring only the Friends class in the response to the client?</p>
  <p>My search in my repository friends:</p>
  <pre><code>    public async Task&lt;IEnumerable&lt;friend&gt;&gt; GetActivefriendsByUser(Guid userId)
      {
          return await Db.friends.AsNoTracking()
              .Include(u =&gt; u.Userapp)
              .Where(k =&gt; k.Active == true)
              .Where(k =&gt; k.UserappId == userId)
              .ToListAsync();
      }
  </code></pre>
  <p>My user Class:</p>
  <pre><code>public class Userapp : Entity
  {
      public string Name { get; set; }
      public string Doc { get; set; }
      public string LastName { get; set; }
      public string Email { get; set; }
      ...
  
      /* EF Relations *
      public IEnumerable&lt;friend&gt; friends { get; set; }
  }
  </code></pre>
  <p>My Friends class:</p>
  <pre><code>public class friend : Entity
  {
      public Guid UserappId { get; set; }
      public string friendUserId { get; set; }
      public string Name { get; set; }
      public string AreaCode { get; set; }
      public string CountryCode { get; set; }
      public string Email { get; set; }
      public bool Active { get; set; }
  
      /* EF Relations */
      public Userapp Userapp { get; set; }
      
  }
  </code></pre>
  <p>the controller receives the user's Id in a get:</p>
  <pre><code>[HttpGet(&quot;{id:guid}&quot;)]
  public async Task&lt;ActionResult&lt;IEnumerable&lt;friendViewModel&gt;&gt;&gt; GetfriendsUser(Guid id)
  {
      if (!ModelState.IsValid) return BadRequest();
  
      var friends = _mapper.Map&lt;IEnumerable&lt;friend&gt;&gt;( await _friendRepository.GetActivefriendsByUser(id));
      if (friends.Count() &gt; 0) return Ok(friends.ToList());
  
      return NotFound(UtilsMessages.friend_NOT_FOUND);
  
  }
  </code></pre>
  <p>I expected something like this, only a list of friends:</p>
  <pre><code>[
      {
          &quot;userAppId&quot;: &quot;1&quot;,
          &quot;friendUserId&quot;: null,
          &quot;name&quot;: &quot;friend1Henrique&quot;,
          &quot;phone&quot;: &quot;123456778&quot;,
          &quot;areaCode&quot;: &quot;31&quot;,
          &quot;countryCode&quot;: &quot;+55&quot;,
          &quot;email&quot;: &quot;emailnaoobrigatorio@email.com&quot;,
          &quot;active&quot;: true,
          &quot;id&quot;: &quot;eb6d0b7e-41ca-47f8-0e49-08d830c1215d&quot;
      },
      {
          &quot;userAppId&quot;: &quot;2&quot;,
          &quot;friendUserId&quot;: null,
          &quot;name&quot;: &quot;friend1Henrique2&quot;,
          &quot;phone&quot;: &quot;1234567478&quot;,
          &quot;areaCode&quot;: &quot;31&quot;,
          &quot;countryCode&quot;: &quot;+55&quot;,
          &quot;email&quot;: &quot;emailnao2obrigatorio@email.com&quot;,
          &quot;active&quot;: true,
          &quot;id&quot;: &quot;eb6d0b7e-41ca-47f8-0e49-08d830c1215d&quot;
      }
  ]
  </code></pre>