Use GUID as PK, and automatic generation of new GUID in OnBeforeEntitySave override

Posts   
 
    
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 19-Oct-2007 17:15:48   

I'm considering to use GUID as PK in some tables of my DB. I'm trying to avoid to explicitly generate and set the new guid on each new entity, so I'm trying to automatically generate and set the new guid inside an override of OnBeforeEntitySave method of CommonEntityBase class

        Protected Overrides Sub OnBeforeEntitySave()
            MyBase.OnBeforeEntitySave()

            Try
                'Check if it's a new entity not already saved in the DB
                If Me.IsNew Then

                    'get the first key column (if I use a GUID as a key column, this will be the only key column of the table)
                    Dim f As IEntityField2 = Me.Fields.PrimaryKeyFields(0)

                    'if the datatype is GUID, and if the field is not initialized, generate a new GUID for the key column
                    If f.DataType Is GetType(Guid) AndAlso f.CurrentValue Is Nothing Then Me.SetNewFieldValue(f.FieldIndex, Guid.NewGuid)

                End If

            Catch ex As Exception
                MsgBox(ex.Message & vbCrLf & ex.StackTrace, MsgBoxStyle.Critical)
            End Try
        End Sub

After some simple test, this seem to work. But I've only done some test with simple object graph.

Do set the key in OnBeforeEntitySave method create some issue with: *) complex object graph? *) entity PK-FK sync? *) entity refetch after save? *) anything others?

Thanks, Max

I'm using

) LLBLGenPro 2.5 Final (September 24th, 2007) (Library Lib 2.5.7.1005)

) Code generation: Adapter/VB.Net 2.0/Standard Templates

) .Net 2.0, VisualStudio 2005 SP1

) Database: Access2k / SQL Server 2k

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 19-Oct-2007 17:42:01   

I think it might be better to set those GUIDs in one of those initialization routines, Especially for entity PK-FK sync.

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 19-Oct-2007 17:55:52   

Walaa wrote:

I think it might be better to set those GUIDs in one of those initialization routines, Especially for entity PK-FK sync.

I've just found that OnBeforeEntitySave is called when the queue for updating the DB are already build. So probably it is not a good place to set a key value.

May be I can generate and set the new GUID key in the ValidateEntityBeforeSave method of the entity validator?

The only other chance I see is to override the SaveEntity/SaveEntityCollection in the DataAccessAdapter... but this is the last resort, because in my project I use 2 different databases, and both must support Access and SQL Server, so I have 4 DB Specific project, and with this solution I will need to replicate (copy/paste) the override 4 times... disappointed

Posts: 254
Joined: 16-Nov-2006
# Posted on: 20-Oct-2007 23:46:09   

Why not simply set the GUID to be auto generated by the database rather than doing this through the LLBLGEN generated code?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39896
Joined: 17-Aug-2003
# Posted on: 21-Oct-2007 11:40:52   

That would work in sqlserver 2005, but in sqlserver 2000, there's no NEWSEQUENTIALID(), so the new GUID isn't retrievable from the db.

The PK value to set is set as every other field in the entity. So I shouldn't worry at all about what happens with the value, as that's been taken care of. so you set field a, b and c to a value and the pk to a guid and you save the entity.

Frans Bouma | Lead developer LLBLGen Pro
Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 22-Oct-2007 09:45:02   

Otis wrote:

That would work in sqlserver 2005, but in sqlserver 2000, there's no NEWSEQUENTIALID(), so the new GUID isn't retrievable from the db.

The PK value to set is set as every other field in the entity. So I shouldn't worry at all about what happens with the value, as that's been taken care of. so you set field a, b and c to a value and the pk to a guid and you save the entity.

ok, but said, for example, that I'm saving a complex graph object: 1) I have many different entities using guid as PK 2) I recursively save the object graph 3) I generate and set the guid PK in an override of OnBeforeEntitySave method

Setting the PK will trigger the PK-FK sync. The PK-FK sync will change data in different entities. But if the PK-FK sync occur due to a change made in the OnBeforeEntitySave method, the update queue is already build. Maybe that the PK-FK sync can change something in the update queue? If this can happen, I'm going to have some problem in setting the PK in the OnBeforeEntitySave method.

Do you think that I can loose some DB update due to the PK-FK sync happening when the update queue is already build?

Thanks, Max

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 22-Oct-2007 11:07:23   

Walaa wrote:

I think it might be better to set those GUIDs in one of those initialization routines, Especially for entity PK-FK sync

Did you try my suggestion? Hint: Use OnInitialized in the CommonEntityBase class.

Max avatar
Max
User
Posts: 221
Joined: 14-Jul-2006
# Posted on: 22-Oct-2007 11:18:38   

Walaa wrote:

Walaa wrote:

I think it might be better to set those GUIDs in one of those initialization routines, Especially for entity PK-FK sync

Did you try my suggestion? Hint: Use OnInitialized int he CommonEntityBase class.

I'm going to try it today, thanks