C# 是否有办法从自有EF实体创建一对多关系?
我是EF的新手,我的模特面临以下情况 我拥有以下实体:C# 是否有办法从自有EF实体创建一对多关系?,c#,entity-framework,C#,Entity Framework,我是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<Logi
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
}
公共类转发构造
{
公共int Id{get;set;}
公共虚拟ICollection FcPorts{get;set;}
///其他属性
}
[拥有]
公共类FcPort
{
公共虚拟ICollection Ltps{get;set;}
///其他属性
}
公共类逻辑替换点
{
公共int Id{get;set;}
///其他属性
}
基于此,我知道可以从所属实体映射一对一关系,但我的问题是,是否可以从同一实体创建一对多引用
编辑:起初我忘了提到我使用的是代码优先方法和实体框架核心。公共类转发构造
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; }
}
{
公共int Id{get;set;}
[反向属性(“转发构造”)]
公共虚拟ICollection FcPorts{get;set;}
///其他属性
}
[拥有]
公共类FcPort
{
[反向属性(“FcPort”)]
公共虚拟ICollection Ltps{get;set;}
public int ForwardingConstructId{get;set;}
公共虚拟转发构造转发构造{get;set;}
///其他属性
}
公共类逻辑替换点
{
公共int Id{get;set;}
public int FcPortId{get;set;}
公共虚拟端口{get;set;}
}
您应该在表中指定外键LogicalTerminationPoint.FcPortId
告诉实体框架创建关系。
然后使用
InverseProperty
属性,您可以指定EF可以使用哪个属性来加载相关子级。非常感谢您的回答@Béranger。我忘了提到我正在使用代码优先的方法,当我尝试您的解决方案时,抛出了一个“InvalidOperationException”,消息是“实体类型'FcPort'无法添加到模型中,因为已经存在同名的弱实体类型。”。另一个问题是FCPort实体在我的模型中最初没有主键。