title: How to select database items recursively in C# using EF Core?
body: <p>I have the following objects:</p>
  <pre><code>public class AccountDTO
  {
      public long Id { get; set; }
  
      [MaxLength(30), MinLength(6)]
      [Required]
      public string Username { get; set; }
  
      [MaxLength(50), MinLength(6)]
      public string Password { get; set; }
  
      [MaxLength(50)]
      public string? SecondaryPassword { get; set; }
  
      public SystemInformationDTO? SystemInformation { get; set; }
  }
  
  public class SystemInformationDTO
  {
      public long Id { get; set; }
  
      [MaxLength(50)]
      public string? Os { get; set; }
  
      [MaxLength(50)]
      public string? Cpu { get; set; }
  
      [MaxLength(50)]
      public string? Gpu { get; set; }
  }
  </code></pre>
  <p>And the following context class:</p>
  <pre><code>public class AccountContext : DbContext
  {
      public DbSet&lt;AccountDTO&gt;? Account { get; set; }
      public DbSet&lt;SystemInformationDTO&gt;? SystemInformation { get; set; }
  
      public AccountContext()
      {
      }
  
      protected override void OnConfiguring(DbContextOptionsBuilder options)
      {
          options.UseSqlServer(&quot;myConnectionString&quot;);
      }
  
      protected override void OnModelCreating(ModelBuilder builder)
      {
          builder.Entity&lt;AccountDTO&gt;()
              .ToTable(&quot;Account&quot;, &quot;Account&quot;);
  
          builder.Entity&lt;AccountDTO&gt;()
              .HasOne(x =&gt; x.SystemInformation);
  
          builder.Entity&lt;SystemInformationDTO&gt;()
              .ToTable(&quot;SystemInformation&quot;, &quot;Account&quot;);
      }
  }
  </code></pre>
  <p>With this, my database is generated as I wanted. I also populated properly.</p>
  <p>Now, I have this repository using my context:</p>
  <pre><code>public class AccountQueriesRepository : IAccountQueriesRepository
  {
      private readonly AccountContext _context;
  
      public AccountQueriesRepository(AccountContext context)
      {
          _context = context;
      }
  
      public AccountDTO? GetAccountByUsername(string username)
      {
          return _context?.Account?
              .Include(x =&gt; x.SystemInformation)
              .FirstOrDefault(x =&gt; x.Username == username);
      }
  
      public AccountDTO? GetAccountById(long id)
      {
          return _context?.Account?
              .Include(x =&gt; x.SystemInformation)
              .FirstOrDefault(x =&gt; x.Id == id);
      }
  }
  </code></pre>
  <p>But when I call the <code>GetAccount</code> (by id or by username, whatever) it does not retrieve the <code>SystemInformation</code>.</p>
  <p>How can I do it without creating separated &quot;selects&quot; in my context?</p>
  <p>Thanks!</p>
  <p>Edit:</p>
  <p><img src="https://i.stack.imgur.com/XB1o9.png" alt="Database screenshot" /></p>