R
RichB
I have created an Entity Framework Context from a database which has a
location ---- country relationship.
As the country table is static, I am creating the Country within my code
using the EnitiyKey as follows:
country.EntityKey = new EntityKey("NearlyThereEntities1.Country",
"countryCode", countryCode);
I then add to the Location as follows:
Location.Country = country;
However when I try to add the Location I get the following error:
The object cannot be added to the ObjectStateManager because it already has
an EntityKey. Use ObjectContext.Attach to attach an object that has an
existing key.
I have tried MyObjectContext.Attach(country); But don't really understand
what the attach method is doing i.e. how to build the relationship between
the Location and country.
I wonder if someone could give me some pointers in how to resolve this.
location ---- country relationship.
As the country table is static, I am creating the Country within my code
using the EnitiyKey as follows:
country.EntityKey = new EntityKey("NearlyThereEntities1.Country",
"countryCode", countryCode);
I then add to the Location as follows:
Location.Country = country;
However when I try to add the Location I get the following error:
The object cannot be added to the ObjectStateManager because it already has
an EntityKey. Use ObjectContext.Attach to attach an object that has an
existing key.
I have tried MyObjectContext.Attach(country); But don't really understand
what the attach method is doing i.e. how to build the relationship between
the Location and country.
I wonder if someone could give me some pointers in how to resolve this.