EDIT: Stub Entities Definition
I have two entity types Subscriber and AddOn
Their definition in data model is as below
Subscriber(#SubscriberID,Name,AddOnID)
AddOn(#AddOnID,Name)
AddOnID column in the Subscriber table references the AddOnID column in AddOn table.
I'm trying to update the AddOn reference of a specific Subscriber entity. Lets say, i want to change the Subscriber#1's AddOn reference to AddOn#5. Here's the code:
Subscriber subscriber = new Subscriber { SubscriberID = 1};
AddOn newAddOn = new AddOn { AddOnID = 5};
using (var context = new TestEntities())
{
context.AttachTo("AddOn", newAddOn);
context.AttachTo("Subscriber", subscriber);
subscriber.Name = "dummy";
subscriber.AddOn = newAddOn;
context.SaveChanges();
}
This throws an exception in line "context.SaveChanges();"
A relationship is being added or deleted from an AssociationSet 'FK-Subscriber-AddOn'. With cardinality constraints, a corresponding 'Subscriber' must also be added or deleted.
When I comment out the "subscriber.AddOn = newAddOn;" line, the update operation works just fine.
So, why i can't just update the reference property like as i update the non-reference one?
Note: I don't know if it is the right way but adding a "context.Refresh(RefreshMode.StoreWins,subscriber);" OR "context.Refresh(RefreshMode.ClietWins,subscriber);" after attach statements make things work.
Why is this behaviour?