views:

13

answers:

1

Hi.

I have an entity (Address) that has a assosiated entity (Country).

  <Table Name="dbo.Address" Member="Address">
    <Type Name="TS.Club.Domain.Model.ValueObject.Address">
      <Column Name="Identifier" Member="Identifier" DbType="UniqueIdentifier NOT NULL IDENTITY" IsPrimaryKey="true" IsDbGenerated="false" AutoSync="OnInsert" />
      <Column Name="ReferenceIdentifier" Member="ReferenceIdentifier" DbType="UniqueIdentifier NOT NULL"/>
      <Column Name="CountryIdentifier" Member="CountryIdentifier" DbType="UniqueIdentifier NOT NULL"/>
      <Column Name="StreetAddress" Member="StreetAddress" DbType="Varchar(512)"/>
      <Column Name="ZipCode" Member="ZipCode" DbType="Varchar(8)"/>
      <Column Name="City" Member="City" DbType="Varchar(512)"/>
      <Column Name="AddressType" Member="Type" DbType="tinyint"/>

      <Association Member="Country" ThisKey="CountryIdentifier" IsForeignKey="true" DeleteRule="NO ACTION" />
    </Type>
  </Table>

My problem is that the country is an "hardcoded" table in my database, so I want it to load when I load the address, but I don't want it to be updated och inserted when I save my address to the database. Right now I get the error: Cannot add an entity with a key that is already in use.

How do I fix this, can't be impossible, but I can't find the solution.

A: 

Are you creating a new Country copy for each Address?
Generally, it is a wrong approach. Try to select an existing entity like this:

Country cc = from c 
db.Countries where c.CountryIdentifier == neededID select c;
Devart