using : SQL Server 2008 R2, Entityframework 4.3.1
my scenario : I read my objects from the database on service side. I am using the T4 Template “Selftracking entities” to create objects from the objectcontext. Those objects are delivered to the client where they are being manipulated and send back to the service.
Via the selftracking i can see what properties have been changed on clientside.
My method to update the entity looks like this :
using ( var transaction = new TransactionScope() )
{
var connection = this.ConnectionManager.GetConnectionstring( AcademyOne.Models.Connection.A1Databases.A1VerwaltungEntity );
using ( var context = new istis.AcademyOne.Models.Models.A1Verwaltung.VerwaltungModelsContext( connection.Connectionstring ) )
{
foreach ( var seminar in seminare )
{
context.Seminar.Attach( seminar );
context.ObjectStateManager.ChangeObjectState( seminar, StateValueConverter.GetEquivalentEntityState( seminar.ChangeTracker.State ) );
}
context.SaveChanges( SaveOptions.DetectChangesBeforeSave );
}
transaction.Complete();
}
My Problem. The Entity “Seminar” has a reference to a “Dozent”. There can be multiple Seminars that have a reference to the same Dozentobject. So when I attach both seminars with the same reference I get the following Exception :
An object with the same key already exists in the ObjectStateManager.
The ObjectStateManager cannot track multiple objects with the same key
Any 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 ?
Solved my problem with a kind of dirty workaround. I Insert/Update now each item on its own and it is working now. Hoped for a batched like update but this does not seem to be possible.
Thanks for your efford anyway