简体   繁体   English

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

[英]SQLite.NET PCL returning 0 in all instances of autoincrement primary key

I am totally not getting this, because I have used this library in Xamarin apps for several years.我完全没有得到这个,因为我已经在 Xamarin 应用程序中使用了这个库好几年了。

I have this base class that contains properties common in all db items:我有这个基类,它包含所有数据库项中的通用属性:

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();
}

Now, I derive from it:现在,我从中得出:

[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;
}

Here's a simplified version of what I'm seeing:这是我所看到的简化版本:

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        
    }
} 

I am obviously missing something, but for the life of me, I can't figure out what...我显然错过了一些东西,但对于我的生活,我无法弄清楚......

Like so many other bizarre things that happen in the Visual Studio Xamarin world, when I went back later, this worked the way all of us expect.就像在 Visual Studio Xamarin 世界中发生的许多其他奇怪的事情一样,当我后来回去时,它以我们所有人期望的方式工作。 I guess Visual Studio was just tired and needed to be restarted.我猜 Visual Studio 只是累了,需要重新启动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM