Another day, another interesting (read: frustrating) development. Once again, I have a setup (still CSLA 2.1.2 - yes, I really wish I could use a more current version) involving a root Project object that contains a number of different collections of associated child objects. I'm getting the following whenever I try to add child objects to an existing root object and then save it when remoting is involved:
Stc.Framework.DataPortalException was unhandled
Message="DataPortal.Update failed (System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_Roll'. Cannot insert duplicate key in object 'dbo.Roll'.
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(TdsParserStateObject stateObj)
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 BusinessObject.Roll.DoInsertUpdate(SqlCommand cm, Guid parentID)
at BusinessObject.Roll.Insert(Project parent)
at BusinessObject.RollCollection.Update(Project parent)
at BusinessObject.Project.UpdateChildren()
at BusinessObject.Project.DataPortal_Update())"
This behaviour does not occur when I'm adding child objects to a new, un-saved root object, nor in either case when I switch my app.config file to not use remoting and just point directly to the database.
And in the case where I'm using my data bound/binded GUI to do the adding and saving via remoting, I get the exception but the child object ends up being saved to the DB anyhow.
Any ideas?
Thanks,
TSK
Do you create any new children after going through the dataportal?
Our business objects have a static class variable that is used to keep track of the next id when creating a child, but this isn't included in the Application Context so if you create objects -1, -2, -3, and -4 on your local machine, and then remote to the server, and it creates another object, it will start at -1 as well which would result in a duplicate.
Dumb question, but do you mean to be adding records to your Roll table? Or are you supposed to be adding records to a ProjectRoll table with a FK to Roll?
E
Where/when are you setting the Id for the Child Objects? Are you sure they are being set to new Guids or are they all Guid.Empty?
E
The child objects set their IDs to Guid.NewGuid() in their respective DataPortal_Create() methods:
<RunLocal()> _
Protected Overrides Sub DataPortal_Create()
_Id = Guid.NewGuid()
_lastChanged = System.DateTime.Now
...
ValidationRules.CheckRules()
End Sub
Pretty run-of-the-mill, non?
Hmmm. Only other thing I can think of is that you transposed your Id's in the DataPortal/SP call, passing the (same) Project.Id instead of the RollId, or your SP has a problem.
E
Copyright (c) Marimer LLC