c# - Delete an Entity with Entity Framework 6.0 -
i trying delete entity using ef6.0 i'm stuck in situation , getting exception. below code snippet i'm running exception
_context.images.remove(image)
i have separate logic gives me entity. i'm getting below exception:
the object cannot deleted because not found in objectstatemanager.
upon googling found out since entity fetched separate context , getting deleted in separate context i'm getting error. don't want fetch entity again. want use fetched entity can't find way so.
what need attach() entity context. give dbcontext knowledge of object. long entity has valid id, you'll fine.
here pseudo-code give idea of you'll need do.
using (var context = new mydbcontext()) { // make context aware of object. context.images.attach(image); // delete object. change internal // state deleted. context.images.remove(image); // finally, save changes context.savechanges(); }
Comments
Post a Comment