In order to resolve this problem, instead of sending list of seminars, send an entity which has one to many relation with seminar entities (of course in case you have such an entity). Thus, when you are calling attach for each updated Seminar entity, a new Dozent entity is being attached to the object context (in fact different objects with same id) which results in a primary key violation. Secondly, I think this problem arises because the state of Dozent entity is Created. ( seminar, StateValueConverter.GetEquivalentEntityState( ) ) The following line shall be commented out.
The following line shall be commented out
using ( var transaction = new TransactionScope() ) Thus, only attaching the updated entity and calling savechanges is enough to save the entity. Since self tracking entities are carrying their own state, if you try to persist any entity, the context will do the related action according to tracking state of the entity. First of all, if you are using Self Tracking Entities, you do not need to change the changetracker state in service. The ObjectStateManager cannot track multiple objects with the same keyĪny ideas how i can solve this problem ? Is there a possibility to attach only the plain object without the reference but including the ForeignKey ID ? Any other approaches I can try ? So when I attach both seminars with the same reference I get the following Exception :Īn object with the same key already exists in the ObjectStateManager. There can be multiple Seminars that have a reference to the same Dozentobject. The Entity "Seminar" has a reference to a "Dozent". Using ( var context = new .A1Verwaltung.VerwaltungModelsContext( connection.Connectionstring ) )Ĭ( seminar, StateValueConverter.GetEquivalentEntityState( ) ) Ĭontext.SaveChanges( SaveOptions.DetectChangesBeforeSave ) Var connection = ( 1Databases.A1VerwaltungEntity )
AUTOTRASH CALLABLE ONLY ON ENTITIES UPDATE
My method to update the entity looks like this : using ( var transaction = new TransactionScope() ) Via the selftracking i can see what properties have been changed on clientside. Those objects are delivered to the client where they are being manipulated and send back to the service. I am using the T4 Template "Selftracking entities" to create objects from the objectcontext. My scenario : I read my objects from the database on service side.
Using : SQL Server 2008 R2, Entityframework 4.3.1