Entity framework 如何使用Fluent API映射这种冗余的父子关系
关于如何双重映射此父子关系的任何建议,其中父对象具有正常的1-many关系(这是有效的),但我需要从父对象到特定子对象的直接1:1,0链接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
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()