I have 2 tables, the relationship between them is linked using field TableId1 and TableId2.
What I want is, if both data are linked, field will filled by primary-key value.
That mean field TableId2 in Table1 will filled by Table2.Id and also field TableId1 in Table2 will filled with Table1.Id.
And, I also want both of field can be empty. That mean nolink of them (that data is Independent).
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; }
}
My Question is, How solve this problem using Data Annotation ?. is that possible ?. The code a above give me error :
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.
Thanks You, Jigu Haslim