Otis wrote:
Could you please paste some code what you tried?
Updating PK values can be done, but not in recursive saves. You also have to fetch the entity, then change the PK value and then save it again.
Sure, here is what I do:
Public Sub SpostaPraticheUtente(ByVal sourceUserId As String, ByVal destinationUserId As String)
Dim checkLists As EntityCollection = CheckListManagement.FetchCollectionForUser(sourceUserId)
Try
adapter.StartTransaction(IsolationLevel.Unspecified, "SPU")
For Each cl As MyCheckListEntity In checkLists
adapter.DeleteEntityCollection(cl.AttAltro)
adapter.DeleteEntityCollection(cl.AttDepositoCommercioIngrosso)
adapter.DeleteEntityCollection(cl.AttLaboratorioProdTrasformazione)
adapter.DeleteEntityCollection(cl.AttProduzionePrimaria)
adapter.DeleteEntityCollection(cl.AttRistorazione)
adapter.DeleteEntityCollection(cl.AttVenditaAlDettaglio)
Next
adapter.DeleteEntityCollection(checkLists)
ChangeUserIdOnAllEntitiesAndMarkAsNew(checkLists, destinationUserId)
For Each cl As MyCheckListEntity In checkLists
ChangeUserIdOnAllEntitiesAndMarkAsNew(cl.AttAltro, destinationUserId)
ChangeUserIdOnAllEntitiesAndMarkAsNew(cl.AttDepositoCommercioIngrosso, destinationUserId)
ChangeUserIdOnAllEntitiesAndMarkAsNew(cl.AttLaboratorioProdTrasformazione, destinationUserId)
ChangeUserIdOnAllEntitiesAndMarkAsNew(cl.AttProduzionePrimaria, destinationUserId)
ChangeUserIdOnAllEntitiesAndMarkAsNew(cl.AttRistorazione, destinationUserId)
ChangeUserIdOnAllEntitiesAndMarkAsNew(cl.AttVenditaAlDettaglio, destinationUserId)
Next
adapter.SaveEntityCollection(checkLists)
adapter.Commit()
Catch ex As Exception
adapter.Rollback()
Throw New JessicaBLException(ex.Message, ex)
End Try
End Sub
Private Sub ChangeUserIdOnAllEntitiesAndMarkAsNew(ByVal coll As EntityCollection, ByVal userId As String)
For Each e As IEntity2 In coll
e.SetNewFieldValue("UserId", userId)
Next
End Sub
As you can see I have a collection of entities , each one has a series of properties with collections of related entities. So, starting from the root entity down I need to update one of the primary key fields. So I thought I would fetch all the data I need, delete all of it, then change the primary key value, and re-add the data with the new value but when I re-add the data an exception is thrown because NULLs are being inserted in the db.
Thanks Frans