I modify the foreign key property on the object in the code, changing only the identifier:
ElementData.ServiceLevelId = parameter.ServiceLevelId;
I found that after that it works, as expected, when the corresponding navigation property ServiceLevelwas nullrandom. If it still contains the "old" object, this change will not get into the database.
That means I need to do
ElementData.ServiceLevelId = parameter.ServiceLevelId;
ElementData.ServiceLevel = null;
Does this mean that changing the object is "stronger" than changing only the identifier? Should I always set the related object to zero in such situations?
Update (for one comment on Copenhauer): The entity in question is a copy (with the mentioned modification) of the existing one. It uses Automapper to copy and displays everything except the primary key and one unrelated property. Automapper creates a shallow copy of AFAIK. Thus, the situation for the copy will be that the updated identifier and the untouched reference to the object will not coincide when it is added to the context. I believe that EF then decides that the "object reference is stronger."
source
share