title: Entity Framework using single address entity with different address type
body: <pre><code>Shop
  public int ShopID {get;set;}
  public string ShopName {get;set;}
  public List&lt;ShopAddresses&gt; addresses {get;set;};
  
  ShopAddresses
  public int ShopId {get;set;}
  public int AddressId {get;set;}
  public Shop Shop {get;set;}
  public Address Address {get;set;};
  public AddressType AddressType {get;set;} //Enum value 1= physical 2 = postal
  
  Address
  public int AddressId {get;set;}
  public string AddressLine1 {get;set;}
  </code></pre>
  
  <p>When I update the Shop object with two ShopAddresses I get the following error.</p>
  
  <blockquote>
    <p>Saving or accepting changes failed because more than one entity of type 'Address' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.</p>
  </blockquote>
  
  <p>The reason why I am getting this error is because my ShopAddresses collection in Shop Class has two Address with the same ID but different address types. This expect as I want to use the same address for different address Type. Entity framework sees that you have two new addresses in ShopAddresses with the same primary key and it throws the error.</p>
  
  <p>Is there a way to configure entityframework code first to for this scenario?</p>
  
  <p>Workaround:</p>
  
  <p>I can avoid this by manually create 1 address using the new keyword and than assigning that to the physical and postal address (ShopAddresses). This approach seems bit overkill.</p>
  
  <p>Another way is to create duplicate address in the database with the same details but different address type. I might go with this approach if entity framework does not support my scenario.</p>