1
我有這樣的場景:實體框架外鍵的多個表的映射
public class Application
{
[Key]
public string Code { get; set; }
}
public class Table1
{
[Key]
public string Table1Code { get; set; }
[Key, ForeignKey("ApplicationObject")]
public string Application { get; set; }
public virtual Application ApplicationObject { get; set; }
}
public class Table2
{
[Key]
public string Table2Code { get; set; }
[Key, ForeignKey("ApplicationObject")]
public string Application { get; set; }
public virtual Application ApplicationObject { get; set; }
}
public class Table3
{
[Key, ForeignKey("Table1Object")]
public string Table1Code { get; set; }
public virtual Table1 Table1Object { get; set; }
[Key, ForeignKey("Table2Object")]
public string Table2Code { get; set; }
public virtual Table2 Table2Object { get; set; }
[Key, ForeignKey("Table1Object")]
public string ApplicationCodeTab1 { get; set; }
[Key, ForeignKey("Table2Object")]
public string ApplicationCodeTab2 { get; set; }
}
在Table 1和Table屬性applicationCode必須是關鍵,因爲我可以有不同的應用程序相同的代碼。
在Table3中我引用了Table1和Table2。如何爲ApplicationCode屬性添加外鍵而不重複該屬性?
例如:
public class Table3
{
[Key, ForeignKey("Table1Object")]
public string Table1Code { get; set; }
public virtual Table1 Table1Object { get; set; }
[Key, ForeignKey("Table2Object")]
public string Table2Code { get; set; }
public virtual Table2 Table2Object { get; set; }
[Key]
public string ApplicationCode { get; set; }
}
的ApplicationCode屬性表3中可以rapresent爲表1和表2中同時外鍵?
我新的EF ...嘗試使用數據標註 '[重點,ForeignKey的( 「Table1Object」),爲了= 1]'' [鍵,ForeignKey的( 「Table2Object」,order = 2)]'。如果它正確告訴我發佈答案 –
嗨@dim mik但項目不建立! 謝謝你的 – Marco
這是有點臭:)我要麼與設計(介紹一個ApplicationTable1接口表)或使用[繼承]多個對多(https://docs.microsoft.com/en-us/aspnet/ mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementation-inheritance-with-the-entity-framework-in-an-asp-net-mvc-application)以Application作爲鑑別器。 –