实体框架6多表到一个外键关系代码第一 [英] Entity Framework 6 multiple table to one foreign key relationship code first

查看:103
本文介绍了实体框架6多表到一个外键关系代码第一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道有没有人可以告诉我如何在EF6中首先使用代码完成下面的代码。





如果将Table_3作为列表添加到Table_1& Table_2在我的实体。 EF自动为Table_3中的两个表生成一个外键列,而不是识别它们的类型相同。



我的模型类设置如下。

  public interface IParent 
{
int ID {get;组; }
列表< Table_3>孩子{get;组; }
}

public class Table_1:IParent
{
[Key]
public int ID {get;组; }
public string Name {get;组; }
public virtual List< Table_3>孩子{get;组;
}

public class Table_2:IParent
{
[Key]
public int ID {get;组; }
public string Name {get;组; }
public virtual List< Table_3>孩子{get;组;
}

public class Table_3
{
[Key]
public int ID {get;组; }
public int ParentID {get;组; }
[ForeignKey(ParentID)]
public virtual IParent Parent {get;组;
}

EF代码首先生成以下





编辑



只是让有同样问题的人知道



我现在通过更改IParent来解决这个问题接口到抽象类
我的课程现在看起来像以下

  [Table(ParentBase)] 
public abstract class ParentBase
{
[Key]
public int ID {get;组; }
public List< Table_3>孩子{get;组; }
}
[Table(Table_1)]
public class Table_1:ParentBase
{
public string Name {get;组;
}
[Table(Table_2)]
public class Table_2:ParentBase
{
public string Name {get;组; }
}
[表(Table_3)]
public class Table_3
{
[Key]
public int ID {get;组; }
public int ParentID {get;组; }
[ForeignKey(ParentID)]
public virtual ParentBase Parent {get;组;
}

表格安排





这样做会很好,如果原来的话可能会更好一些。

解决方案

我也有这个问题,从开始就使用抽象类而不是接口。
我的问题是我的table_3有两个导航属性:
一个是公共虚拟Table_1,另一个是公共虚拟Table_2,然后EF只是提供了这些额外的外键列
我合并了两个导航属性一个到
public virtual parentbase {get; set;}。然后它工作。希望这有帮助。



侧面注意,建议在public List中添加虚拟关键字List Children {get;组; }在parentbase类中,因为在以前的例子中,它已经是这样了。谢谢你发布这个,我也遇到这个问题。


I am wondering if anyone could advise me on how to accomplish the below using code first in EF6

If I add the Table_3 as a List on to Table_1 & Table_2 in my entities. EF automatically generates a foreign key column for both tables in Table_3 instead of recognizing that they are of the same type.

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

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

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

解决方案

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;}. And then it worked. Hope this helps.

Side Note,Would suggest to add virtual keyword on public List Children { get; set; } in parentbase class, because in your previous example , it was already like that.

Thanks for posting this, i came across this issue too.

这篇关于实体框架6多表到一个外键关系代码第一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆