我是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
}
基于this answer,我知道可以映射拥有实体的一对一关系,但是我的问题是是否可以从同一实体创建一对多引用实体
编辑:最初,我忘了提到我使用的是代码优先方法和Entity Framework Core。
答案 0 :(得分:0)
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可以用来加载相关子项的属性。