QuoteReplyTopic: Unable to save a changed entity that has a many to many relation Posted: 25-Jun-2010 at 1:42pm
Hi Greg,
Your test harness reproduces the problem im having with my test.
When the PostNavPropRetrievalSaveCompleted gets called the op.Exception.RemoteExceptionDetails contains the same database exception as in my test. Imho the server trys to add the already exsisting territories of the employee to the dbo.EmployeeTerritoryNoPayload table.
Thanks,
Nico Schoemaker.
Below the RemoteExceptionDetails :
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_EmployeeTerritoryNoPayload'. Cannot insert duplicate key in object 'dbo.EmployeeTerritoryNoPayload'. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) --- End of inner exception stack trace --- at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at IdeaBlade.EntityModel.Edm.EdmSaveHelper.ProcessSaves(IEnumerable`1 groupsByType) at IdeaBlade.EntityModel.Edm.EdmSaveHelper.SaveWithinContext() at IdeaBlade.EntityModel.Edm.EdmSaveHelper.Save()
I think so - I added statements to retrieve the changed Employee's Territories before attempting to save the Employee, and the save did blow up, though not with very helpful error information. I'm going to convert my example to a test case and send it on to the chief developer.
By the way, if you want a copy of the test harness I'm using, submit a Support Request http://www.ideablade.com/TechSupport/CustomerSupportRequestForm.aspx and I'll send it to you. Things that involve somebody else's UI controls are extremely difficult to debug - too much "black box" stuff going on.
> so we don't have effects of the bug related to that
Correct, got a test harness that is not related to the other many to many issue ;-)
The diference between your and my test harness is that the binding of the datagrid automaticly gets the related Teritory objects through Path=CurrentItem.Territories when i select an Employee in the GUI.
I think this error is somehow an artifact of your UI. I can run the following code successfully in my demo harness. I am assuming you've left the getters for both Employee.Territories and Territory.Employees as public, so we don't have effects of the bug related to that.
public void TestManyToManySave() {
_localOutput.Append(String.Format("[{0}] Started...\n", _nameTestManyToManySave));
Utility.ResetEntityManager(_em1);
Im unable to save a changed entity that has a many to many relation.
Steps to reproduce:
NorthwindIB model, many to many relationship without payload between Employee.Territories and Territory.Employees.
Display a master detail setup where Employee is the master showing Teritories as details and enable saving of a changed Employee.
Select an employee with teritories and change the Employee its firstname and save the changes.
The change of Employee is not saved now which can be checked on the database and the EntitySaveOperation callback of the SaveChangesAsync method of the manager.
What i have noticed within the exception i got back from the server (EntitySaveOperation) is that on the server DevForce/EF trys to add the Teritories of the Employee to the database again.This shouldnt be happening since only Employee has been changed.
Below the serverexception found within the EntitySaveOperation:
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_EmployeeTerritoryNoPayload'. Cannot insert duplicate key in object 'dbo.EmployeeTerritoryNoPayload'. The statement has been terminated. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async) at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues) at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) --- End of inner exception stack trace --- at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter) at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at IdeaBlade.EntityModel.Edm.EdmSaveHelper.ProcessSaves(IEnumerable`1 groupsByType) at IdeaBlade.EntityModel.Edm.EdmSaveHelper.Save()
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum