2016-12-05 59 views
0

我必須模擬2個實體:Book和Label。 Book類包含書籍信息和對包含標籤文本的Label類的引用。因此,我必須實現從Book類到Label類的單向導航,因爲當我想象一個圖書實體時,我想閱讀Label值,但是當我想象一個Label時,我不想獲取關於Book的任何信息。實體框架:帶數據註釋的單向導航

我發現很多Fluent API的例子,但我需要用Data Annotations來實現。有任何想法嗎?

public class Book 
{ 
    [Key] 
    public int id { get; set; } 

    public string Title { get; set; } 

    //What about label? How to navigate from Book to Label? 

} 

public class Label 
{ 
    [Key] 
    public int id { get; set; } 

    public string Name { get; set; } 
} 

回答

0

你不需要任何註釋,如下的EntityFramework約定,只添加Label類型的Book和同爲其他類的屬性,就像這樣:

public class Book 
{ 
    [Key] 
    public int id { get; set; } 

    public string Title { get; set; } 

    public Label Label {get;set;} 

} 

public class Label 
{ 
    [Key] 
    public int id { get; set; } 

    public string Name { get; set; } 

    public Book Book { get; set; } 
}