.net - Trouble updating self-tracking entities -


i'm using self-tracking entities in wcf client-server application. wcf service returns various entities, , these can updated using corresponding update methods.

this worked while, i'm having problems. keep focus, i'l limit discussion specific case, simplified bare essentials.

one of tables called systemdefinition. has no foreign keys, 1 other table (route) has foreign key it. consequently, has single navigation property in entity model (called routes). other columns scalars. table, , corresponding entities, has primary key column called id, of type guid. database sql server compact v3.5.

to reproduce problem, can:

  1. retrieve single systemdefinition entity using wcf service's getsystem() method
  2. in client, call markasdeleted() on entity
  3. call updatesystem(), passing entity parameter

the code in updatesystem() (non-essential code deleted clarity):

_objectcontext.systemdefinitions.applychanges(system); _objectcontext.savechanges(); 

the entity retrieved no include() clause, means routes collection empty (and anyway, error still occurs if there no rows in route foreign keys systemdefinition). systemdefinition entity pass update method only entity in graph. yet still following exception:

invalidoperationexception: acceptchanges cannot continue because object's key values conflict object in objectstatemanager. make sure key values unique before calling acceptchanges.

the exception thrown first method call (applychanges). objectcontext fresh, new 1 created each method call.

i have debugged code way down throws (in objectcontext.fixupkey()), code makes little sense me, , there no comments in microsoft's source code indicate condition causes means.

certainly, message misleading? there's single entity involved in update. might going on?

ps. found forum post suggesting overriding gethashcode() , equals() methods on entity classes might help. make sense if error caused objectstatemanager not being able determine entity update in fact same entity in context. tried out (using partial classes), ufortunately didn't help. i'm lost. suggestions welcome.

i suspect _objectcontext contains entity instances try apply changes from. since entities received external client, never same (in terms of instance reference) instances context knows about.

this makes sense exception thrown: context ends 2 different instances containing same key values.

to solve make sure work fresh context.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -