Question

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 ?

Was it helpful?

Solution 2

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

OTHER TIPS

Your code has some problems. First of all, if you are using Self Tracking Entities, you do not need to change the changetracker state in service. 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. Thus, only attaching the updated entity and calling savechanges is enough to save the entity.

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 )
                {
                    //The following line shall be commented out
                    //context.Seminar.Attach( seminar );

                    context.Seminar.ApplyChanges( seminar );

                    //The following line shall be commented out.
                    //context.ObjectStateManager.ChangeObjectState( seminar, StateValueConverter.GetEquivalentEntityState( seminar.ChangeTracker.State ) );

                }
                context.SaveChanges( SaveOptions.DetectChangesBeforeSave );
            }             

            transaction.Complete();
        }

Secondly, I think this problem arises because the state of Dozent entity is Created. 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.

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).

Or if you do not have a parent entity, try setting foreign key values of seminar entities without setting the related Dozent navigation property. You should first ensure that foreign key properties exist in the EDMX model file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top