I have an entity Locations
with its primary key consisting of Longitude
and Latitude
as double
.
From another entity object I want to remove the reference to an entity object in Locations
but every time I try to set the reference to null I get an OptimisticConcurrencyException
.
using (MyModelContainer context = new MyModelContainer())
{
Note note = context.Notes.Single(n => n.NID == NoteUpdate.NID);
note.LocationReference.Load();
note.LocationReference = null;
context.saveChanges();
}
But it is not working. The same is with note.LocationReference.Value = null
.
How can I set the reference to null or its default value?
I believe the purpose is to update db so that location column of note row is set to null here, so detaching is not going to work. I think replacing note.LocationReference = null;
with note.Location = null;
in the code above should work
Have you tried to delete the reference? I believe that this will delete the parent row, but would be curious to find out if it only deletes the reference if you pass in the object tied to the note.
context.DeleteObject(note.LocationReference);
Typically, the optimistic concurrency error means that the value has changed since you last loaded the object, but it sounds like that is not the problem. But, to verify, nobody could be modifying this in between running the code, right?
Last, have you tried to set the State?
context.Entry(note).State = EntityState.Added
In your Note class, add:
public int? LocationReferenceId {get; set;}
To remove the reference do:
Note.LocationReferenceId = null;
And then save the Note Object back to the db
I recon that should do the trick.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.