![](/img/trans.png)
[英]How to use Composite Primary key in Xamarin Forms and SQLite.Net
[英]SQLite.NET PCL returning 0 in all instances of autoincrement primary key
我完全沒有得到這個,因為我已經在 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 只是累了,需要重新啟動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.