简体   繁体   English

有没有办法从拥有的 EF 实体创建一对多关系?

[英]Is there a way to create a one to many relationship from an owned EF entity?

I'm new to EF and I'm facing the following situation with my model.我是 EF 的新手,我的模型面临以下情况。

I have the following entities:我有以下实体:

public class ForwardingConstruct
{
  public int Id {get; set;}
  public virtual ICollection<FcPort>  FcPorts { get; set; }
  /// Other attributes

}

[Owned]
public class FcPort
{
  public virtual ICollection<LogicalTerminationPoint>  Ltps { get; set; }
  /// Other attributes

}

public class LogicalTerminationPoint
{
  public int Id {get; set;}
  /// Other attributes

}

Based on this answer I know that it is posible to map a one-to-one relation from the owned entity, but my question is if it is posible to create a one-to-many reference from the same entity基于此答案,我知道可以从拥有的实体映射一对一关系,但我的问题是是否可以从同一实体创建一对多引用

Edit: Initially I forgot to mention that I'm using code-first approach and Entity Framework Core.编辑:最初我忘了提到我正在使用代码优先方法和实体框架核心。

public class ForwardingConstruct
{
  public int Id {get; set;}
  [InverseProperty("ForwardingConstruct")]
  public virtual ICollection<FcPort>  FcPorts { get; set; }
  /// Other attributes

}

[Owned]
public class FcPort
{
  [InverseProperty("FcPort")]
  public virtual ICollection<LogicalTerminationPoint>  Ltps { get; set; }
  public int ForwardingConstructId { get; set; }
  public virtual ForwardingConstruct ForwardingConstruct { get; set; }
  /// Other attributes

}

public class LogicalTerminationPoint
{
  public int Id {get; set;}
  public int FcPortId { get; set; }
  public virtual FcPort FcPort { get; set; }

}

You should specify the foreign key in your tables.您应该在表中指定外键。 LogicalTerminationPoint.FcPortId is what tells to entity framework to create a relationship. LogicalTerminationPoint.FcPortId告诉实体框架创建关系。 Then with the InverseProperty attribute you can specify which property EF can use to load the relevant children.然后使用InverseProperty属性,您可以指定 EF 可以使用哪个属性来加载相关子项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM