title: Prevent circular references when getting result from database
body: <p>If I have the following simple model:</p>
  
  <pre><code>    public class Company
      {
          public Guid CompanyId { get; set; }
  
          public ICollection&lt;CompanyUser&gt; Users { get; set; }
      }
  
      public class CompanyUser
      {
          public Guid CompanyId { get; set; }
  
          public Guid UserId { get; set; }
  
          public Company Company { get; set; }
  
          public User User { get; set; }
      }
  
      public class User
      {
          public Guid UserId { get; set; }
  
          public ICollection&lt;CompanyUser&gt; Companies { get; set; }
      }
  </code></pre>
  
  <p>To get a list of companies + their users + the user object I run the following query:</p>
  
  <pre><code>return await _dataContext.Companies
      .Include(m =&gt; m.Users)
      .ThenInclude(m =&gt; m.User)
      .OrderBy(m =&gt; m.Name)
      .ToListAsync();
  </code></pre>
  
  <p>The results work, but I use a mapper to map the results to a view model by going recurisvely through the model.</p>
  
  <p>What happens is that the <code>Company</code> object has a reference to a list of <code>CompanyUser</code>, in each of those <code>CompanyUser</code> objects we have a <code>Company</code> which has a list of <code>CompanyUser</code> again, which just keeps repeating until we get a stack overflow.</p>
  
  <p><a href="https://i.stack.imgur.com/NowhL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/NowhL.png" alt="enter image description here"></a></p>
  
  <p>The mapper is a very simple one:</p>
  
  <pre><code>var results = companies.ToViewModel&lt;Company, CompanyViewModel&gt;();
  </code></pre>
  
  <pre><code>public static IList&lt;TModel&gt; ToViewModel&lt;TEntity, TModel&gt;(this IEnumerable&lt;TEntity&gt; entities)
      where TEntity : class
      where TModel : class, IViewModel&lt;TEntity&gt;, new()
  {
      return entities?.Select(entity =&gt; entity.ToViewModel&lt;TEntity, TModel&gt;()).ToList();
  }
  
  public static TModel ToViewModel&lt;TEntity, TModel&gt;(this TEntity entity)
      where TEntity : class
      where TModel : class, IViewModel&lt;TEntity&gt;, new()
  {
      if (entity == null)
      {
          return null;
      }
  
      var model = new TModel();
      model.ToViewModel(entity);
      return model;
  }
  </code></pre>
  
  <pre><code>public interface IViewModel&lt;in TEntity&gt;
      where TEntity : class
  {
      void ToViewModel(TEntity entity);
  }
  
  public class CompanyViewModel : IViewModel&lt;Company&gt;
  {
      public Guid CompanyId { get; set; }
  
      public IList&lt;CompanyUserViewModel&gt; Users { get; set; }
  
      public void ToViewModel(Company entity)
      {
          CompanyId = entity.CompanyId;
          Users = entity.Users.ToViewModel&lt;CompanyUser, CompanyUserViewModel&gt;();
      }
  }
  
  public class CompanyUserViewModel : IViewModel&lt;CompanyUser&gt;
  {
      public Guid CompanyId { get; set; }
  
      public Guid UserId { get; set; }
  
      public CompanyViewModel Company { get; set; }
  
      public UserViewModel User { get; set; }
  
      public void ToViewModel(CompanyUser entity)
      {
          CompanyId = entity.CompanyId;
          UserId = entity.UserId;
          Company = entity.Company.ToViewModel&lt;Company, CompanyViewModel&gt;();
          User = entity.User.ToViewModel&lt;User, UserViewModel&gt;();
      }
  }
  
  public class UserViewModel : IViewModel&lt;User&gt;
  {
      public Guid UserId { get; set; }
  
      public void ToViewModel(User entity)
      {
          UserId = entity.Id;
      }
  }
  </code></pre>
  
  <p>Is there a way to prevent these references to be resolved?</p>