title: Entity Framework: insert with one-to-one reference
body: <p>I'm having a bit trouble inserting into a mssql database using Entity Framework. There's two tables that I want to insert into, where one of table 1s fields is a foreign key in table2.</p>
  
  <p>This is the code I have so far:</p>
  
  <pre><code>Media media = null;
  foreach(POI p in poiList)
  {
      media = new Media() 
      {
          Path = p.ImagePath,
          Title = p.Title
      };
  
      if (media != null &amp;&amp; !context.Media.Any(me =&gt; me.Title == p.ImageTitle))
      {
          context.AddToMedia(media);
          context.SaveChanges();
      }
  
      PointOfInterest poi = new PointOfInterest()
      {
          Altitude = 2000.0,
          ID = p.ID,
          Latitude = p.Latitude,
          Longitude = p.Longitude,
          LatitudeRoute = p.LatitudeRoute,
          LongitudeRoute = p.LongitudeRoute,
          Description = p.Description,
          Title = p.Title,
          DefaultImageID = media.ID,
      };    
      context.AddToPointOfInterest(poi);
  }
  context.SaveChanges();
  </code></pre>
  
  <p>The following gives me this error:</p>
  
  <p><em>An object with the same key already exists in the ObjectStateManagerAn object with the        same key already exists in the ObjectStateManager</em></p>
  
  <p>I'm still learning how to use the entity framework, so I don't even know if this would be the right approach to insert into two referenced tables. </p>
  
  <p>Can anyone enlighten me on this? :) Any help would be greatly appreciated!</p>
  
  <p>Thanks!</p>