[英]Entity Framework 6 multiple table to one foreign key relationship code first
I am wondering if anyone could advise me on how to accomplish the below using code first in EF6 我想知道是否有人可以建议我如何在EF6中首先使用代码完成以下操作
If I add the Table_3 as a List on to Table_1 & Table_2 in my entities. 如果我将Table_3作为List添加到我的实体中的Table_1和Table_2。 EF automatically generates a foreign key column for both tables in Table_3 instead of recognizing that they are of the same type.
EF会自动为Table_3中的两个表生成外键列,而不是识别它们属于同一类型。
My model classes are set as follows. 我的模型类设置如下。
public interface IParent
{
int ID { get; set; }
List<Table_3> Children { get; set; }
}
public class Table_1 : IParent
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual List<Table_3> Children { get; set; }
}
public class Table_2 : IParent
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
public virtual List<Table_3> Children { get; set; }
}
public class Table_3
{
[Key]
public int ID { get; set; }
public int ParentID { get; set; }
[ForeignKey("ParentID")]
public virtual IParent Parent { get; set; }
}
EF code first generates the below EF代码首先生成以下内容
Just to let anyone having the same problems know 只是为了让任何有同样问题的人知道
I have now resolved this by changing the IParent interface to an abstract class my classes now look like the following 我现在通过将IParent接口更改为抽象类来解决这个问题,我的类现在看起来如下所示
[Table("ParentBase")]
public abstract class ParentBase
{
[Key]
public int ID { get; set; }
public List<Table_3> Children { get; set; }
}
[Table("Table_1")]
public class Table_1 : ParentBase
{
public string Name { get; set; }
}
[Table("Table_2")]
public class Table_2 : ParentBase
{
public string Name { get; set; }
}
[Table("Table_3")]
public class Table_3
{
[Key]
public int ID { get; set; }
public int ParentID { get; set; }
[ForeignKey("ParentID")]
public virtual ParentBase Parent { get; set; }
}
with a table arrangement of 与表安排
this will work although it would have been nicer if the original could have been met. 如果能够满足原件的要求会更好。
You can also do like the following where you make a 1 to many relationship between Table_1
and Table_2
with Table_3
respectively: 您还可以执行以下操作,其中分别使用
Table_3
在Table_1
和Table_2
之间Table_1
1对多的关系:
modelBuilder.Entity<Table_3>().HasOptional(/*Nav Prop*/).WithMany(m => m.Table_3s).HasForeignKey(f => f.ParentId).WillCascadeOnDelete(false);
modelBuilder.Entity<Table_3>().HasOptional(/*Nav Prop*/).WithMany(m => m.Table_3s).HasForeignKey(f => f.ParentId).WillCascadeOnDelete(false);
Let me know if anymore clarification is required. 如果需要进一步澄清,请与我们联系。
I had this problem too, and I used abstract class instead of interface from the beginning. 我也有这个问题,我从一开始就使用抽象类而不是接口。 The problem for mine was my table_3 have two navigation properties: one is public virtual Table_1, another is public virtual Table_2, and then EF just provisioned these extra foreign key columns, I merged the two navigation properties into one to
public virtual parentbase {get;set;}.
我的问题是我的table_3有两个导航属性:一个是公共虚拟Table_1,另一个是公共虚拟Table_2,然后EF刚刚配置了这些额外的外键列,我将两个导航属性合并为一个
public virtual parentbase {get;set;}.
And then it worked. 然后它奏效了。 Hope this helps.
希望这可以帮助。
Side Note,Would suggest to add virtual keyword on public List Children { get; 侧注,建议在公共列表子项上添加虚拟关键字{get; set;
组; } in parentbase class, because in your previous example , it was already like that.
在parentbase类中,因为在前面的示例中,它已经是这样了。
Thanks for posting this, i came across this issue too. 感谢发布这个,我也遇到了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.