2016-01-24 92 views
0

我正在使用實體框架代碼第一個框架。我正在創造前衛的鑰匙。但是當我從表中訪問數據時,我只是沒有外鍵信息的表信息。外鍵缺失 - 實體框架代碼第一個

public class X : EntityData { 
    public int a; 
    public int b; 
    public ICollection<Y> YID { get; set; } //This will create a foreign key of X_ID in Y table 
} 

public class Y : EntityData { 
    public int c; 
    public int d; 
} 

現在,如果我試圖從前端訪問Y表,它不返回我的外鍵是X_ID。但它正在返回c和d。在sql表中,我可以將Y表中的X_ID看作外鍵。請讓我知道從服務器和客戶端獲取外鍵表信息的最佳方式。 但幫助鏈接
http://myapp.azure-mobile.net/help/Api/GET-tables-Y顯示與表的外鍵的詳細信息,但實際的服務沒有返回外鍵的詳細信息,甚至沒有外鍵的ID。
在此先感謝。

回答

0

您的Y類像這樣來定義外鍵關係(和你可能想在你的X類主鍵):

public class X : EntityData { 
    [Key] 
    public int a { get; set; } 
    public int b { get; set; } 
    public ICollection<Y> YID { get; set; } //This will create a foreign key of X_ID in Y table 
} 

public class Y : EntityData { 
    public int c { get; set; } 
    public int d { get; set; } 

    public int XId { get; set; } 
    [ForeignKey("XId")] 
    public virtual X XReference { get; set; } 
} 
+0

ForeignKey的註釋是找不到的。它在visual basic中給出了編譯錯誤。 –

+0

'ForeignKey'屬性位於命名空間'System.ComponentModel.DataAnnotations.Schema'中。你輸入了嗎? –

+0

是的,我輸入它。但它仍然不起作用。 –