Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有办法从自有EF实体创建一对多关系?_C#_Entity Framework - Fatal编程技术网

C# 是否有办法从自有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

我是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

}
公共类转发构造
{
公共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实体在我的模型中最初没有主键。