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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -