title: Entity Framework - adding an existing entity to one to one relationship in seed method
body: <h2>My Domain Classes:</h2>
  
  <pre><code>public class Address
  {
      [Key]
      public virtual string AddressId { get; set; }
  
      public virtual Site Site { get; set; }
  }
  
  public class Site
  {
      [Key]
      public virtual int SiteId { get; set; }
  
      public virtual Address Address { get; set; }
  }
  </code></pre>
  
  <h2>Mapping using Fluent API:</h2>
  
  <pre><code>public class SiteMappings : EntityTypeConfiguration&lt;Site&gt;
  {
      public SiteMappings()
      {
          HasRequired(s =&gt; s.Address)
          .WithOptional(a =&gt; a.Site)
          .Map(s =&gt; s.MapKey("AddressId"))
          .WillCascadeOnDelete(false);
      }
  }
  </code></pre>
  
  <h2>Seed Method:</h2>
  
  <pre><code>var addresses = new List&lt;Address&gt;
  {
      new Address { AddressId = "1" }
  };
  addresses.ForEach(s =&gt; context.Addresses.AddOrUpdate(p =&gt; p.AddressId, s));
  
  var sites = new List&lt;Site&gt;
  {
      new Site { SiteId = 1, Address = addresses.Single(s =&gt; s.AddressId.Equals("1"))}
  };
  sites.ForEach(s =&gt; context.Sites.AddOrUpdate(p =&gt; p.SiteId, s));
  </code></pre>
  
  <h2>Error:</h2>
  
  <pre><code>Violation of PRIMARY KEY constraint 'PK_dbo.Addresses'. Cannot insert duplicate key in object 'dbo.Addresses'. The duplicate key value is (1)
  </code></pre>
  
  <p>It seems to be when I try to add a new "site", new "address" also get inserted.
  How to avoid this? How to insert an existing "address" in DBcontext I just added before. 
  I'm new to Entity Framework and really appreciate your help.
  Thanks in advance!</p>