Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 如何使用Fluent API映射这种冗余的父子关系_Entity Framework_Ef Code First_Entity Framework 6_Ef Fluent Api - Fatal编程技术网

Entity framework 如何使用Fluent API映射这种冗余的父子关系

Entity framework 如何使用Fluent API映射这种冗余的父子关系,entity-framework,ef-code-first,entity-framework-6,ef-fluent-api,Entity Framework,Ef Code First,Entity Framework 6,Ef Fluent Api,关于如何双重映射此父子关系的任何建议,其中父对象具有正常的1-many关系(这是有效的),但我需要从父对象到特定子对象的直接1:1,0链接 Public Class Source <Key()> Public Property SourceID As Int32 Public Property SourceFields As List(Of SourceField) Public Property InboundMatchKeySourceFiel

关于如何双重映射此父子关系的任何建议,其中父对象具有正常的1-many关系(这是有效的),但我需要从父对象到特定子对象的直接1:1,0链接

Public Class Source
    <Key()>
    Public Property SourceID As Int32

    Public Property SourceFields As List(Of SourceField)

    Public Property InboundMatchKeySourceFieldID As Nullable(Of Int32)
    Public Property InboundMatchKeySourceField As SourceField
这是我尝试进行其他直接映射的失败尝试(不起作用):

这将产生“MetaDataException”:

指定的架构无效。错误:关系 未加载“\uuuuuu.Source\u InboundMatchKeySourceField” 因为类型“\\\.SourceField”不可用

.WithRequired(…)
返回
ForeignKeyNavigationPropertyConfiguration
的实例。您需要使用map方法将此关系映射到一个键,否则EF将尝试为您创建一个键

因此,您要查找的代码是:

modelBuilder.Entity(Of Source
).HasOptional(
    Function(S) S.InboundMatchKeySourceField
).WithRequired(
    Function(SF) SF.Source
) _
.Map(Function (x) x.MapKey("InboundMatchKeySourceFieldID"))

不能对这两个关系使用相同的属性
SourceField.Source

从实体框架的角度来看,存在歧义:

  • 如果设置
    someSourceField.Source
    ,是指
    someSourceField
    被添加到
    thatSource.SourceFields
    ,还是
    thatSource.InboundMatchKeySourceField
    被设置,还是两者都被设置
  • 如果
    someSource.InboundMatchKeySourceField.Source!=一些来源

也许您可以在不使用导航属性的情况下创建第二个映射,并使用验证来确保
SourceFields.Contains(InboundMatchKeySourceField)
thisSource.InboundMatchKeySourceField.Source==thisSource
(如果需要):

modelBuilder.Entity(Of Source
    ).HasOptional(
        Function(S) S.InboundMatchKeySourceField
    ).WithRequired()

很好的建议,唉,我仍然收到相同的错误消息
modelBuilder.Entity(Of Source
    ).HasOptional(
        Function(S) S.InboundMatchKeySourceField
    ).WithRequired(
        Function(SF) SF.Source
    )
modelBuilder.Entity(Of Source
).HasOptional(
    Function(S) S.InboundMatchKeySourceField
).WithRequired(
    Function(SF) SF.Source
) _
.Map(Function (x) x.MapKey("InboundMatchKeySourceFieldID"))
modelBuilder.Entity(Of Source
    ).HasOptional(
        Function(S) S.InboundMatchKeySourceField
    ).WithRequired()