Can't retrieve auto generated primary key using Vici Coolstorage

84 views Asked by At

I'm using Vici Coolstorage in a Windows Forms project to access a SQLite database. In every table in my database there is a field called ID defined as INTEGER PRIMARY KEY, so it is an auto increment field.

I'm trying to retrieve the value of that field after I store the object in the database, but I always get the value 0 instead of the real ID. The Vici Coolstorage documentation states that "if the primary key is defined as an autonumber (identity) field in the database, your can retrieve the generated primary key after the object is saved", but that doesn't seem to be true unless I'm doing something wrong. Please help me. This code will reproduce the problem:

<MapTo("Company")> Public MustInherit Class Company
    Inherits CSObject(Of Company, Integer)
    Public MustOverride ReadOnly Property ID As Integer
    Public MustOverride Property Name As String
End Class

Sub SomeMethod()
    Dim C As Company = Company.[New]
    C.Name = "Some name"
    C.Save()
    MessageBox.Show(C.ID)  'This always prints 0!!!
End Sub

Thank you!

1

There are 1 answers

0
Rahul P Nath On

Had faced this issue and figured out that setting the identity attribute on the field solved this.

[Identity]
public int Id
{
    get { return (int)GetField("Id"); }
}