Search code examples
sql-servervb.netguidpetapoco

Saving a new object with an empty guid PetaPoco fails to do an insert


I am trying to save a new object to the database using petapoco. I have an object that uses a Guid for the primary key.

Here is an example of the objects primary key property.

<PetaPoco.Column()> _
Public ReadOnly Property Primkey() As Guid Implements IStandardDocument.Primkey
    Get
        Return (_primkey)
    End Get
End Property

Here is an example of the save method.

database.Save("Reservations", "Primkey", reservation)

The sql that is generated is an update with a primkey of 00000000-0000-0000-0000-000000000000 instead of an insert. So new records are never inserted.

If I have an existing object with a valid guid the update and delete all work correctly.

What am I doing wrong for the save case?


Solution

  • I was able to make this work by removing the type from the primkey property. Setting the primkey to null will now return null rather than an empty guid.

    PetaPoco now sees the primarykey field is a null and generate the correct insert statement rather than generating an update statement for primkey 00000000-0000-0000-0000-000000000000.