简体   繁体   English

实体框架6首先将多个表与一个外键关系代码

[英]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代码首先生成以下内容

在此输入图像描述

Edit 编辑

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_3Table_1Table_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.

相关问题 实体框架代码中相同表的多个外键优先 - Multiple Foreign Key for Same table in Entity Framework Code First 实体框架代码复合密钥的第一对一关系 - Entity Framework Code First One to One relationship on composite key 实体框架一对一可选外键代码优先流利映射 - Entity Framework one to optional foreign key code first fluent mapping 实体框架代码优先的多对多表外键 - Entity Framework code-first foreign key to many to many table 实体框架-代码优先-共享主键一对多关系 - Entity Framework - Code first - One to Many relationship with shared primary key 多个派生类中的实体框架 6 Code First TPH 外键 - Entity Framework 6 Code First TPH Foreign Key in multiple derived classes 实体框架代码首先是多层父子外键 - Entity Framework code first multiple tier parent child foreign key Entity Framework Core - 代码优先 - 两个外键 - 一张表 - Entity Framework Core - Code First - Two Foreign Keys - One Table 使用实体框架中的数据库优先方法,如何仅从具有多个外键关系的单个表中获取数据? - Using Database first approach in Entity framework, how to fetch the data from a single table only which has multiple foreign key relationship? 实体框架核心数据库优先-到一个表的多个外键 - Entity Framework Core Database First - Multiple Foreign keys to one table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM