2014-10-18 61 views
0

有沒有辦法在SQLite中爲c#使用註釋複合鍵?下面的代碼拋出一個異常,稱該表有不止一個主鍵:SQLite for WinRT多個主鍵?

userDatabase.CreateTable<MyObject>(); 

//class file 
[SQLite.Table("MyObject")] 
public class MyObject 
{ 
    [SQLite.PrimaryKey] 
    public int IdOne { get; set; } 

    [SQLite.PrimaryKey] 
    public int IdTwo { get; set; } 
} 
+0

這裏與Entity Framework有什麼關係? – TomTom 2014-10-19 08:43:16

回答

0

我有一個解決方案。這不是最好的解決方案,但它對我有用。

這個想法是手動製作組合鍵。 所以,在你的情況下,它聲明:

userDatabase.CreateTable<MyObject>(); 

//class file 
[SQLite.Table("MyObject")] 
public class MyObject 
{ 
    public int IdOne { get; set; } 
    public int IdTwo { get; set; } 

    [SQLite.PrimaryKey] 
    public string auxCompositeKey {get; set;} 
} 

然後,當你創建對象,只是讓德auxCompositeKey如下:

oMyObject.auxCompositeKey = IdOne.ToString()+ IdTwo.ToString( )。

+0

什麼,如果將有兩個記錄,與 IdOne = 1,IdTwo = 23 和: IdOne = 12,IdTwo = 3 ;)? – Namek 2016-07-28 13:40:04

+0

它會打擊!大聲笑。它需要一些更新才能正常工作..但其主意就是這樣。 – ovamendocino 2016-07-29 15:51:03