Weird behaviour when using remoting

Weird behaviour when using remoting

Old forum URL: forums.lhotka.net/forums/t/4522.aspx


TheSquirrelKing posted on Wednesday, March 19, 2008

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

dlabar replied on Wednesday, March 19, 2008

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.

 

dlabar replied on Wednesday, March 19, 2008

Nevermind you're getting a sql error, not a .Net error.

TheSquirrelKing replied on Thursday, March 20, 2008

This shouldn't be the case here since we're using GUIDs as the ID and primary key for our objects. As such, I don't understand how the SQL Server thinks we're getting duplicates unless things are getting inserted twice somehow? I'm not entirely sure why remoting would cause this.

Any other thoughts or ideas would be appreciated!

Thanks,
-TSK

SomeGuy replied on Thursday, March 20, 2008

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

 

TheSquirrelKing replied on Thursday, March 20, 2008

That's not a dumb question. But yes, the intention is to add to the Roll table. The Roll table is setup with an ID (a GUID) as the primary key and has a ParentID (also a GUID) along with the timestamp and its other properties. This is the same for all of the child objects and they are all displaying the exceptional behaviour I mentioned in the OP.

SomeGuy replied on Thursday, March 20, 2008

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

 

TheSquirrelKing replied on Thursday, March 20, 2008

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?

SomeGuy replied on Thursday, March 20, 2008

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