tomomomo
tomomomo

Reputation: 157

The context is already tracking the entity - I know but how to solve it?

I am writing a WPF client application and use WCF Data Service to communicate with a database. I have the following situation: I add a new Policy, it has an attached object House and each House has an Address. In standard Entity Framework application it wasn't a problem but here I need to add relationships between entites. This is my code:

context.AddToPolicySet(Policy);
context.AddToAdressSet(Address);
context.AddRelatedObject(Address, "HouseSet", House); 
context.AddRelatedObject(Policy, "HouseSet", House); // !!!!!!!!!!!!
Policy.HouseSet.Add(House);
House.PolicySet = Policy;
Address.HouseSet.Add(House);
House.AdressSet = Address;

Now, I understand that the context is already tracking the entity. But how to solve this problem? If I remove the fourth line then I get the error "Insert statement violetes foreign key constraint...". It seems to me that I need to attach a House to the Policy and a House to the Address. But my way is obviously the wrong way. What is the right one? :)

Upvotes: 0

Views: 3473

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109251

Use the AddLink method:

context.AddLink(Policy, "HouseSet", House);

EDIT

It appears to be

context.SetLink(House, "PolicySet", Policy);

Upvotes: 1

Related Questions