简体   繁体   中英

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.

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. Then with the InverseProperty attribute you can specify which property EF can use to load the relevant children.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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