Can't manage to set a many-to-one association to null and save an entity -
say have:
public class event { public int eventid { get; set; } ... public sponsor sponsor { get; set; } } public class sponsor { get; set; } public int sponsorid { get; set; } ... } so event can have, not have, sponsor. can create event, set sponsor , save fine.
what cannot figure out how make work is
event event = context.events.find(id); event.sponsor = null; context.savechanges(); the above doesn't make change sponsorid column in database.
try make "sponsor" virtual property. depending on configuration, ef doesn't know whether have loaded event.sponsor property , if haven't doesn't know sponsor = null actuall change. there 2 kinds of proxys generated ef , depending on pocos ef hasn't generated self-tracking proxy event-class (for example, navigation property sponsor isn't virtual), when compares snapshots of previous event entity, "sponsor" wasn't loaded , updated one, "sponser" set null, assumes didn't change it's value.
Comments
Post a Comment