我嘗試使用WP8的SQLite庫。WF8&C#&SQLite&autoincrement&Fail
予定義的表:
class ShoppingItem
{
[SQLite.PrimaryKey]
public int Id {get; set;}
public string Name {get; set;}
public string Shop {get; set;}
public bool isActive {get; set;}
}
根據http://www.sqlite.org/autoinc.html,一個值被分配給由Id
源碼自動如果Id
沒有值已經給出。對?
所以我試圖通過
using (var db = new SQLiteConnection(m_DatabasePath)) {
db.Insert(new ShoppingItem() {
Name = anItem,
Shop = aShop,
isActive = aIsActive,
});
}
當我插入第一個項目中插入新的條目,它得到ID爲0那麼,爲什麼不呢? 當我插入第二個項目時,我得到一個帶有令人敬畏的消息「約束」的SQLiteException。 那麼,如何在不給ID的情況下插入新條目?
btw .: 作爲替代解決方案,我試圖給id添加一個空值,但是這導致了編譯錯誤。 :(
...
db.Insert(new ShoppingItem() {
Id = null, // <- does not compile
Name = anItem,
...
是您的ID在你的表自動增量中您的數據庫? – Guillaume 2013-03-18 10:02:44
不,但我不認爲這是必要的,根據http://www.sqlite.org/autoinc.html – 2013-03-18 10:05:29