title: How do I delete related objects in Entity framework code first database?
body: <p>DBContext class is</p>
  
  <pre><code>public class VGDB : DbContext
      {        
          public DbSet&lt;Planet&gt; Planets { get; set; }
      }
  </code></pre>
  
  <p>And model looks like:</p>
  
  <pre><code>public class Planet
      {
          [Key]
          public int Id { get; set; }
          public string Name { get; set; }
  
          ...
  
          public List&lt;Building&gt; Constructions { get; set; } 
      }
  
  
  public class Building
      {
          [Key]
          public int Id { get; set; }
          public decimal Lvl { get; set; }
          public string Type { get; set; }
      }
  </code></pre>
  
  <p>Repository class:</p>
  
  <pre><code>public class VGDBRepository
      {
          private readonly VGDB _vgdb;
          ...
          public void RemovePlanets()
          {
              foreach (Planet planet in _vgdb.Planets)
              {
                  _vgdb.Planets.Remove(planet);
              }
              _vgdb.SaveChanges();
          }
          ...
      }
  </code></pre>
  
  <p>Entity Framework creates database with two tables: <code>Planets</code> and <code>Buildings</code>, related by <code>Planet_Id</code> field. When I call <code>RemovePlanets()</code> method of my <code>VGDBRepository</code> class it removes planets record from Planets table and sets <code>Planet_Id</code> field of all buildings, related with deleted planets, in Buildings table to null but not deletes them, so I have redundant records in database. I use <code>code-first</code> strategy to create database. How can I force Entity Framework to remove such type of related data???</p>