CodeFirst 配置错误创建原因?

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

    }

这些实体之间的关系是: EntityParent :

  • 有一个或多个 EntityChild 类型的孩子(第一个关系)

  • 具有零个或一个 EntityChild 类型的转换(第二个关系)

目标是在 EntityParent 中具有以下属性:

  • 儿童名单。

  • 包含 Start 位置的 EntityChildren。

在 EntityChildren 中的属性:

  • 以该实体为起始位置的 EntityParent 列表

  • 此 EntityChildren 的 EntityParent

  • EntityChildrenSource

  • EntityChildrenTarget

请提供任何帮助


慕莱坞森
浏览 174回答 2
2回答

largeQ

在 EF Core 中,每个关系都包含 0、1 或 2 个导航属性。在大多数情况下,EF Core 可以自动确定关系及其关联的导航属性。但有时它不能,所以它会抛出异常,并希望您通过数据注释、流式 API 或两者的组合来明确指定。在这种特殊情况下,异常消息告诉您 EF Core 无法确定EntityChildren.TransformedParents集合导航属性表示的关系。您可以通过使用[InverseProperty]数据注释将其与ParentEntity.TransformationEntity参考导航属性配对来解决它:[InverseProperty(nameof(EntityParent.TransformationEntity))]public virtual ICollection<EntityParent> TransformedParents { get; set; }在这种特殊情况下,这应该足够了。Fluent API 更加灵活,因为它们允许完全配置关系的所有方面 - 主体、依赖、导航属性、依赖 FK 属性、主体 PK 属性、必需/可选、级联删除行为等。相应的 fluent 配置如下:modelBuilder.Entity<EntityParent>()&nbsp; &nbsp; .HasOne(p => p.TransformationEntity)&nbsp; &nbsp; .WithMany(c => c.TransformedParents)&nbsp; &nbsp; .HasForeignKey(p => p.TransformationEntityId) // optional (by convention)&nbsp; &nbsp; .IsRequired(false) // optional (by convention)&nbsp; &nbsp; .OnDelete(DeleteBehavior.ClientSetNull) // optional (by convention)&nbsp; &nbsp; ;

qq_花开花谢_0

public class EntityChildren{&nbsp; &nbsp; &nbsp;public virtual ICollection<EntityParent> TransformedParents { get; set; }&nbsp; &nbsp;&nbsp;和public class EntityParent{&nbsp; &nbsp; &nbsp;public virtual ICollection<EntityChildren> Childrens { get; set; }创建 EF Core 不支持的多对多关系。必须有一个中间班来解决这个问题比如一个类中间类 ParentChildren&nbsp;public class ParentChildren&nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; public int ParentId { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public EntityParent Parent{ get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int ChildId { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public EntityChild Child{ get; set; }&nbsp;}然后,ICollection<ParentChildren>在你的EntityParent和EntityChild数据库上下文protected override void OnModelCreating(ModelBuilder modelBuilder)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.Entity<EntityParent>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasKey(x => x.Id);&nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.Entity<EntityChild>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasKey(x => x.Id);&nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.Entity<ParentChildren>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasKey(x => new { x.ParentId , x.ChildId });&nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.Entity<ParentChildren>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasOne(x => x.Parent)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WithMany(m => m.Childrens)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasForeignKey(x => x.ParentId);&nbsp; &nbsp; &nbsp; &nbsp; modelBuilder.Entity<ParentChildren>()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasOne(x => x.Child)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .WithMany(e => e.TransformedParents)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .HasForeignKey(x => x.ChildId);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP