SQLite.NET PCL在自动增量主键的所有实例中返回0

问题描述

我完全没有得到这个,因为我已经在Xamarin应用程序中使用了这个库已有多年了。

我有一个包含所有数据库项目共有的属性的基类:

public class BaseItem
{
    [PrimaryKey,AutoIncrement]
    public int ID { get; set; } = 0;        // sqlite ID
    public long CreatedTimeSeconds { get; set; } = DateTime.Now.ToUnixTimeSeconds();
    public long ModifiedTimeSeconds { get; set; } = DateTime.Now.ToUnixTimeSeconds();
}

现在,我从中得出:

[Table("CategoryTable")]
public class Category : BaseItem
{
    public int CategoryTypeID { get; set; } = (int)CategoryType.Invalid;
    public string Name { get; set; } = string.Empty;
    public string Description { get; set; } = string.Empty;
}

这是我所看到的简化版本:

public class DBWorld
{
    IsqliteService sqlite { get { return DependencyService.Get<IsqliteService>(); } }

    private readonly sqliteConnection _conn;
    
    public DBWorld()
    {
        _conn = sqlite.GetConnection("myapp.sqlite");    
    }

    public void TestThis()
    {
        _conn.CreateTable<Category>();
        
        var category = new Category();
        category.Name = "This Should Work";
        
        int recCount = connection.Insert(category);
        // at this point recCount shows as 1,and category.ID shows as zero.
        // I thought Insert was supposed to set the autoincrement primary key 
        
        // regardless,it should be set in the database,right? So...
        var categoryList = connection.Query<Category>($"SELECT * FROM {DBConstants.CategoryTableName}");
        // at this point categoryList[0] contains all the expected values,except ID = 0        
    }
} 

我显然很想念什么,但是对于我的一生,我不知道该怎么办...

解决方法

就像在Visual Studio Xamarin世界中发生的许多其他奇异的事情一样,当我稍后返回时,这按我们所有人期望的方式工作。我想Visual Studio太累了,需要重新启动。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...