【发布时间】:2018-09-10 08:56:06
【问题描述】:
我是 EntityFramework Core Code 第一个数据库生成的初学者,我对两个实体的关系配置有疑问:
public class EntityParent
{
public int Id { get; set; }
public string Name { get; set; }
//Navigation properties to the EntityChildren which have info of start position.
[ForeignKey("TransformationEntity")]
public int? TransformationEntityId { get; set; }
public virtual EntityChildren TransformationEntity { get; set; }
//Navigation property : List of childrens
public virtual ICollection<EntityChildren> Childrens { get; set; }
}
public class EntityChildren
{
public int Id { get; set; }
public string Name { get; set; }
public int StartPosition { get; set; }
//List of EntityParents which have this EntityChildren as the start position
public virtual ICollection<EntityParent> TransformedParents { get; set; }
//Relation one-to-one(this same table)
[ForeignKey("EntityChildrenSource")]
public int? Cadrage { get; set; }
public virtual EntityChildren EntityChildrenSource { get; set; }
public virtual EntityChildren EntityChildrenTarget { get; set; }
//Navigation property to EntityParent
[ForeignKey("Parent")]
public int Parent_FK { get; set; }
public virtual EntityParent Parent { get; set; }
}
这些实体之间的关系是: 实体父级:
- 有一个或多个 EntityChild 类型的子级(第一个关系)
- 有零个或一个 EntityChild 类型的转换(第二个关系)
目标是在 EntityParent 中拥有属性:
- 孩子名单。
- 包含起始位置的 EntityChildren。
在 EntityChildren 中的属性:
- 以该实体为起始位置的 EntityParent 列表
- 此 EntityChildren 的 EntityParent
- EntityChildrenSource
- EntityChildrenTarget
但是在执行生成数据库脚本的命令时出现以下错误:
System.Reflection.TargetInvocationException:调用的目标已抛出异常。 ---> System.InvalidOperationException: 无法确定由“ICollection”类型的导航属性“EntityChildren.TransformedParents”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。 在 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder) 在 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuil 德) 在 Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext 上下文,IConventionSetBuilder 约定SetBuilder,IModelValidator v 验证器)
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: **Unable to determine the relationship represented by navigation property 'EntityChildren.TransformedParents' of type 'ICollection<EntityParent>'**. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
在 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder) 在 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.ImmediateConventionScope.OnModelBuilt(InternalModelBuilder modelBuil 德) 在 Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext 上下文,IConventionSetBuilder 约定SetBuilder,IModelValidator v 验证器)
请帮忙
【问题讨论】:
标签: c# asp.net-mvc entity-framework-core