Hmmm... Validation Error

Posts   
 
    
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 04-Aug-2007 21:31:29   

Hi all. Long time no post.

Using ver 2.0 and adapter.

Within the ValidateEntityBeforeSave(involvedEntity) I'm running the following code:

psuedocode:

public void SaveEntity()
{
    ... ParentEntity.Fields.EntityState = Fetched
    ... ParentEntity.ChildEntity.Fields.EntityState = "New", ChildEntity.IsNew, IsDirty = true, ChildEntity.KeyField = 0 (unassigned, database auto-generated key value)
}

public override ValidateEntityBeforeSave(IEntityCore involvedEntity)
{
    foreach (ChildEntity child in involvedEntity.Children)
  {
        if (child.aProperty == x) do a thing... // ORMOutOfSyncError!!!!
        // Now: child.Fields.EntityState = OutOfSync, Child.IsNew, IsDirty = false
        // Now. child.KeyField = aValue !  An auto-value has been assigned
  }
}

Oops! Generating an OutOfSync error. It appears that NEW (child.IsNew = true) children records are being SAVED prior to the ValidateEntityBeforeSave() call. Child.IsNew and IsDirty changes from true to false.

!! Interesting: ChildField.KeyField gets assigned a value from the db (autoincrement = true). But, ORMOutOfSync generates an error and an apparent rollback, because the new keyValue does NOT appear in the DB.

Please be assured that my code is not touching the Children Records after SaveEntity() is called. There are no other persistence calls made after SaveEntity().

Does LLBLGen perform an interim persistence call to children records in some sort of transaction?

Thanks!!!!

Jeff

Posts: 254
Joined: 16-Nov-2006
# Posted on: 04-Aug-2007 23:51:14   

Please could you attach a trace file whilst executing this code and the code just before the ValidateEntityBeforeSave method is called.

Enable all LLBLGen trace switches and output to a text file.

Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 05-Aug-2007 04:22:51   

MattAdamson wrote:

Please could you attach a trace file whilst executing this code and the code just before the ValidateEntityBeforeSave method is called.

Enable all LLBLGen trace switches and output to a text file.

Thanks Matt. But I was able to resolve the problem by refetching the entity in the SaveEntity() call. Interesting. It appears that LLBLGen DOES perform some behind-the-scenes persistence calls that might affect BeforeSave validation calls for complex parent/child graphs.

Perhaps Frans can verify this.

Thanks for the help!

Jeff

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 05-Aug-2007 10:37:31   

Inserts happen before updates. This can happen in the scenario where the children are new and the entity to validate isn't new but updated. This thus means that the insert of the children is performed before the update.

Frans Bouma | Lead developer LLBLGen Pro
Jeff M
User
Posts: 250
Joined: 04-Aug-2004
# Posted on: 05-Aug-2007 21:46:33   

Otis wrote:

Inserts happen before updates. This can happen in the scenario where the children are new and the entity to validate isn't new but updated. This thus means that the insert of the children is performed before the update.

This causes some interesting errors if the parent tries to validate the children records during its BeforeSave event. However, everything seems to work if the SaveEntity method has Refetch=true.

Jeff