c# - Bring back DbContext.Detach() method with an extension method (EF5) -


this question has answer here:

there no detach(object entity) on dbcontext in entity framework 5.

to detach entity, state needs changed. maybe missing something, seems less intuitive , readable using detach method:

context.entry(myentity).state = entitystate.detached; 

i tempted create extension method bring detach method:

public static void detach(this myentities context, object entity) {     context.entry(entity).state = entitystate.detached; } 

what reason why microsoft removed dbcontext.detach() method in ef 5?

removing detach method (dbcontext) api has logic because detach doesn't operate on object graph detaches single object pass method. different other methods change object state:

  • attach attaches supplied object including related objects in object graph of navigation properties
  • add adds supplied object including related objects context
  • remove deletes supplied object including related objects have been configured cascading delete

on other hand setting state manually modified, added or deleted acts on supplied object, not on related objects. case calling detach method of objectcontext. more consequent detach object via setting state detached in line behaviour of other state changes because setting other state influences supplied object without related objects.

dbcontext - among other features - intended make working entity framework easier. old detach method more confusing , behaviour not many developers expect. (here , here 2 references confusion , complexities involved in detaching object.) in opinion wasn't wrong step remove dbcontext api.

well, can write own extension method did or access underlying objectcontext via adapter if want have detach method.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -