Muhammad Saifullah
Muhammad Saifullah

Reputation: 4292

AutoIncrement is not working with sqlite-net Client in windows phone universal App

I am using sqlite-net for sqlite in my windows phone universal application I have following class with table and column attributes and using async connection to add data in sqlite all is working fine, But the Id column is not auto incremented neither it is a primary key. Does any one else face this issue? following is my code.

 [Table("Account")]
public class Account
{
    [PrimaryKey, AutoIncrement]
    [Column("Id")]
    public int Id { get; set; }

    [Column("FirstName")]
    public string FirstName { get; set; }

    [Column("LastName")]
    public string LastName { get; set; }

    [Ignore]
    public string FullName { get; set; }
  }


  SQLite.SQLiteAsyncConnection conn = new SQLite.SQLiteAsyncConnection("database.sqlite");
        var result = await conn.CreateTableAsync<Account>();

        var _accountList = new Account();
        _accountList.FirstName = "Name 1";
        _accountList.LastName = "------------";
        var result1 = await conn.InsertAsync(_accountList);

        _accountList = new Account();
        _accountList.FirstName = "Name 2";
        _accountList.LastName = "------------";
         result1 = await conn.InsertAsync(_accountList);

         var list = await conn.Table<Account>().ToListAsync();

         var item = list;

on reading the table Id is always 0 for all records. Is there any way to fix this?

Upvotes: 2

Views: 2880

Answers (4)

tbear
tbear

Reputation: 135

If you're using sqlite-net-pcl nuget, it might be because your using InsertOrReplace. See https://stackoverflow.com/a/77755612/14161217

Upvotes: 0

max
max

Reputation: 41

This is an old post, but maybe my answer can help someone, almost i hope so. First declare your Id field as nullable:

[PrimaryKey, AutoIncrement]
[Column("Id")]
public int? Id { get; set; }

Is important that your primary key is nullable, otherwise it will be sent to the Insert method with the value of "0" and SQLite won't change an already existing integer value, and the next insert you'll receive an error, becouse you're trying to put another "0" index in the table.

And, in your CreateTable section, specify the createFlags argument, that if omitted, defaults to "none".

This is how i do it. "_dbPath" is the path to the database file in IsolatedStorage:

using (_connection = new SQLite.SQLiteConnection(_dbPath))
{
   _connection.CreateTable<T>(SQLite.CreateFlags.ImplicitPK | SQLite.CreateFlags.AutoIncPK);
}

This will made your Id field an indexed primary key, autoincremental.

Upvotes: 4

konradowy
konradowy

Reputation: 1570

I had a similar issue.

Try to reset simulator:

Step 1: Goto C:\Program Files (x86)\Microsoft XDE\8.1\
Step 2: Run XdeCleanup.exe

and rerun you app.

It happened for me because I had created a database without primary key and auto increment and then added it to the code. But somehow the state without PK&AI was cached in simulator.

Upvotes: 0

user3898309
user3898309

Reputation: 21

I think using the below code will help you.

 [SQLite.PrimaryKey, SQLite.AutoIncrement]
        public int Id { get; set; }

Upvotes: 0

Related Questions