我有2個表格,它們之間的關係使用字段表格Id1和表格Id2鏈接。在EF.6中使用數據註釋的OneOrZero關係?
我想要的是,如果兩個數據都鏈接,則字段將填充主鍵值。
,在平均場TableId2表1將填補Table2.Id還現場TableId1在表2將充滿Table1.Id。
而且,我也希望兩個字段都可以是空的。這意味着他們不知道(數據是獨立的)。
public class Table1
{
[Key]
public int Id { get; set; }
public string table1_desc { get; set; }
public int? TableId2 { get; set; }
[ForeignKey("TableId2")]
public Table2 Table2 { get; set; }
}
public class Table2
{
[Key]
public int Id { get; set; }
public string Table2_desc { get; set; }
public int? TableId1 { get; set; }
[ForeignKey("TableId1")]
public Table1 Table1 { get; set; }
}
我的問題是,如何解決使用數據註解這個問題?那可能嗎 ?。該代碼上面給我的錯誤:
Unable to determine the principal end of an association between the types 'ConsoleApplication1.Data2' and 'ConsoleApplication1.Data1'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
謝謝你, 祭告Haslim