title: When do I need to specify DbSet in EF's DbContext?
body: <p>I'm a little bit confused. Until today I thought that every table (used by EF) must be specified in <code>DbContext</code> class. But it looks like I need <strong>ONLY one</strong>! really?</p>
  
  <p>Let me explain, Here's my DbContext:</p>
  
  <pre><code>public class MyDbContext : DbContext
  {
      public MyDbContext()
          : base("name=MyDbContext")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          Database.SetInitializer&lt;MyDbContext&gt;(null);
          base.OnModelCreating(modelBuilder);
      }    
  
      public DbSet&lt;Table1&gt; Table1 { get; set; }
      public DbSet&lt;Table2&gt; Table2 { get; set; }
      public DbSet&lt;Table3&gt; Table3 { get; set; }
      public DbSet&lt;Table4&gt; Table4 { get; set; }
      public DbSet&lt;Table5&gt; Table5 { get; set; }
  }
  </code></pre>
  
  <p>Here are two sample tables, connected 1:many</p>
  
  <pre><code>[Table("Table1")]
  public class Table1
  {
      [Key]
      [Column("Table1Id", TypeName = "uniqueidentifier")]
      public int Table1Id { get; set; }
  
      [Column("Table2Id", TypeName = "int")]
      public int Table2Id { get; set; }
  
      [ForeignKey("Table2Id")]
      public Table2 Table2 { get; set; }
  }
  
  [Table("Table2")]
  public class Table2
  {
      public Table2()
      {
          this.Table1s = new HashSet&lt;Table1&gt;();
      }
  
      [Key]
      [Column("Table2Id", TypeName = "int")]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Table2Id { get; set; }
  
      public ICollection&lt;Table1&gt; Table1s { get; set; }
  } 
  </code></pre>
  
  <p>Easy. Now, I want to query all Table2s with corresponding Table1s. I do:</p>
  
  <pre><code>var tables2 = fni.Set&lt;Table2&gt;()
      .Include(i =&gt; i.Table1s)
      .Where(t =&gt; t.Table2Id == 123456).ToList();
  </code></pre>
  
  <p>It all works, but I was shocked, when I discovered by accident, that it works even with this DbContext:</p>
  
  <pre><code>public class MyDbContext : DbContext
  {
      public MyDbContext()
          : base("name=MyDbContext")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          Database.SetInitializer&lt;MyDbContext&gt;(null);
          base.OnModelCreating(modelBuilder);
      }    
  
  
      public DbSet&lt;Table1&gt; Table1 { get; set; }
  }
  </code></pre>
  
  <p>or this..</p>
  
  <pre><code>public class MyDbContext : DbContext
  {
      public MyDbContext()
          : base("name=MyDbContext")
      {
      }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          Database.SetInitializer&lt;MyDbContext&gt;(null);
          base.OnModelCreating(modelBuilder);
      }    
  
  
      public DbSet&lt;Table2&gt; Table2 { get; set; }
  }
  </code></pre>
  
  <p>Can you explain to me, why does it work? How?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT. It's not include. I was able to do:</p>
  
  <pre><code>var tables2 = fni.Set&lt;Table2&gt;()
      .Where(t =&gt; t.Table2Id == 123456).ToList();
  </code></pre>
  
  <p>having only this: <code>public DbSet&lt;Table1&gt; Table1 { get; set; }</code> in DbContext. It's not even Table2! They are connected via FK (definitions didn't change). So that would mean, that you must have only one table from one "chain" of tables. Is that correct?</p>