[英]Is there a way to create a one to many relationship from an owned EF entity?
我是 EF 的新手,我的模型面临以下情况。
我有以下实体:
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
}
基于此答案,我知道可以从拥有的实体映射一对一关系,但我的问题是是否可以从同一实体创建一对多引用
编辑:最初我忘了提到我正在使用代码优先方法和实体框架核心。
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; }
}
您应该在表中指定外键。 LogicalTerminationPoint.FcPortId
告诉实体框架创建关系。 然后使用InverseProperty
属性,您可以指定 EF 可以使用哪个属性来加载相关子项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.