c# - EntityFramework 5 - InvalidOperationException when accessing DbContext.ChangeTracker.Entries -
in dbcontext implementation if have method called "ismodified". used application show kind of "dirty" state. within method access changetracker of dbcontext shown below.
if access changetracker.entries while data loaded / materialized database invalidoperationexception because internal stateentry collection has changed.
is there way around without using try / catch. or there maybe more efficient way of tracking modified state of context?
public bool ismodified() { return this.changetracker.entries().any(e => e.state != entitystate.unchanged); }
convert dbcontext objectcontext , try following implementation of ismodified:
var context = new yourdbcontext(); var adapter = (iobjectcontextadapter)context; var objectcontext = adapter.objectcontext; ... public bool ismodified() { bool modified = context.objectstatemanager.getobjectstateentries(~entitystate.unchanged); .any(); return modified; } you can try handle context.objectstatemanager.objectstatemanagerchanged event , update property in event. should more elegant.
Comments
Post a Comment