Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite-net-pcl - Always returning ID as 0 (Xamarin)

I recently moved across from SQLite.NET to SQLite-net-pcl due to the Android 7.0 SQlite issue.

In the process I have updated all my libraries and all is working well with regards to insert/drop etc.

The only issue I have is that every time I retrieve an item from the DB it always has an ID of 0. This is causing problems with updating items. Has anyone had this issue before?

 [SQLite.PrimaryKey, SQLite.AutoIncrement]
    public int ID { get; set; }
    public string objectId { get; set; }
    public DateTime createdAt { get; set; }
    public DateTime updatedAt { get; set; }
    public string Title { get; set; }

Thanks in advance.

like image 305
Phill Wiggins Avatar asked Jan 20 '17 11:01

Phill Wiggins


2 Answers

Please try using this, this worked for me

using SQLite.Net.Attributes;

  [PrimaryKey, AutoIncrement]
    public int? Id { get; set; }
like image 112
user8122933 Avatar answered Oct 17 '22 16:10

user8122933


Had almost the same situation. with all rows returning id of 0

class ToDo : Java.Lang.Object
{
    [PrimaryKey, AutoIncrement]
    public int ID { get; }

except I simply had forgotten to type set; in the property.

one thing that might help someone with this sorta problem is using the GetMapping() method to get some info on the mapping used when CreateTable() is used to create the table. as example:

        Toast.MakeText(this, db.GetMapping<ToDo>().TableName.ToString(), ToastLength.Long).Show();
        Toast.MakeText(this, db.GetMapping<ToDo>().HasAutoIncPK.ToString(), ToastLength.Long).Show();

using this I found out that AutoIncrement (HasAutoIncPK = false) wasn't being set on my table.

like image 26
eris discord Avatar answered Oct 17 '22 17:10

eris discord