1

我需要主键从 1000 开始。怎么办?

public class LoginTable
{

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

    public string info { get; set; }
    public string info2 { get; set; }



}
4

1 回答 1

1

您可以修改sqlite_sequence表格并分配您想要的任何起点。如果LoginTable尚未插入,则需要插入新sqlite_sequence记录,否则需要更新现有sqlite_sequence记录。

sqlite_sequence仅包含两个字段 (nameseq),并且不包含主键或任何索引。name是您的表的名称,并且seq是当前使用的序列号,下一个插入将在使用它之前增加它。

例子:

using (var db = new SQLiteConnection("foo.sqlite"))
{
    db.CreateTable<LoginTable>();

    db.Execute("insert into sqlite_sequence (name, seq) values (?, ?);", new object[] { "LoginTable", 999 });

    // if LoginTable records already exist, then the `sqlite_sequence` record exists for this table and you need to update it...
    //db.Execute("update sqlite_sequence set seq = ? where name = ?;", new object[] { 999, "LoginTable"});

    var login = new LoginTable { info = "info1....", info2 = "info2....." };
    db.Insert(login);
    db.Insert(login);
    db.Insert(login);
    foreach (var item in db.Table<LoginTable>())
    {
        Console.WriteLine(item.id);
    }
}

输出:

1000
1001
1002
于 2017-08-11T00:54:53.920 回答