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作为鉴别器。 –