title: C# Entity Framework adding one to many relationship entries
body: <p>In my <code>DbContext</code> class, I have 2 tables that have a one-to-many relationship.</p>
  
  <p>Firstly I create one <code>Location</code> object, then I try to add a <code>ParkingSpot</code> object that references to the <code>Location</code> object, but in a database, it is not adding the <code>ParkingSpot</code> object to the database. What do I do wrong?</p>
  
  <p><code>DbContext</code> class:</p>
  
  <pre><code>public class ParkingDBContext : DbContext
  {
      public ParkingDBContext() : base("DbConnectionString")
      {
      }
  
      public DbSet&lt;Location&gt; Locations { get; set; }
      public DbSet&lt;ParkingSpot&gt; ParkingSpots { get; set; }
  }
  </code></pre>
  
  <p><code>Location</code> class:</p>
  
  <pre><code>public class Location
  {
      public Location()
      {
          ParkingSpots = new List&lt;ParkingSpot&gt;();
      }
  
      public string Id { get; set; }
      public virtual ICollection&lt;ParkingSpot&gt; ParkingSpots { get; set; }
  }
  </code></pre>
  
  <p><code>ParkingSpot</code> class:</p>
  
  <pre><code>public class ParkingSpot
  {
      public string Id { get; set; }
      public virtual Location Location { get; set; }
  }
  </code></pre>
  
  <p>The way that I'm trying to add new <code>ParkingSpot</code>:*</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(string Id,string locationId)
  {
      try
      {
          Location loc = dbContext.Locations.FirstOrDefault(x =&gt; x.Id == locationId);
          dbContext.ParkingSpots.Add(new ParkingSpot { Id = Id, Location = loc });
          dbContext.SaveChanges();
  
          return RedirectToAction("Index");
      }
      catch
      {
          return View();
      }
  }
  </code></pre>