c# - EF6: Modifying an entity property with a foreign key relation - Do I need to change the Id or the related object or both? -
i modifiying foreign key property on entity in code, modifiying id only:
elementdata.servicelevelid = parameter.servicelevelid;
i have found, after persisting, works expected, when corresponding navigation property servicelevel
null
accident. if still holds "old" object, change not hit database.
this means, need do
elementdata.servicelevelid = parameter.servicelevelid; elementdata.servicelevel = null; //force update database
does mean, changing object "stronger" changing id only? should set related object null in such situations?
update (per tim copenhaver's comment): entity in question copy (with mentioned modification) of existing one. uses automapper copying, , maps except primary key , 1 unrelated property. automapper creates shallow copy afaik. thus, situation copy updated id , untouched object reference not match @ moment of adding context. guess, ef decides "object reference stronger".
changing either property work long data mapping correct. ef smart enough see of properties has changed , ignore other one. have careful, though - if elementdata.servicelevel.id not equal elementdata.servicelevelid, obscure errors.
if you're having trouble not saving, mapping layer not correct. can troubleshoot if can post mapping elementdata class , more code around how you're doing save.
Comments
Post a Comment