亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

CodeFirst 配置錯誤創建原因?

CodeFirst 配置錯誤創建原因?

C#
慕萊塢森 2022-01-09 17:34:31
我是 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 的 EntityParentEntityChildrenSourceEntityChildrenTarget請提供任何幫助
查看完整描述

2 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

在 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>()

    .HasOne(p => p.TransformationEntity)

    .WithMany(c => c.TransformedParents)

    .HasForeignKey(p => p.TransformationEntityId) // optional (by convention)

    .IsRequired(false) // optional (by convention)

    .OnDelete(DeleteBehavior.ClientSetNull) // optional (by convention)

    ;


查看完整回答
反對 回復 2022-01-09
?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

public class EntityChildren

{

     public virtual ICollection<EntityParent> TransformedParents { get; set; }    


public class EntityParent

{

     public virtual ICollection<EntityChildren> Childrens { get; set; }

創建 EF Core 不支持的多對多關系。


必須有一個中間班來解決這個問題


比如一個類中間類 ParentChildren


 public class ParentChildren

 {

        public int ParentId { get; set; }

        public EntityParent Parent{ get; set; }


        public int ChildId { get; set; }

        public EntityChild Child{ get; set; }

 }

然后,ICollection<ParentChildren>在你的EntityParent和EntityChild


數據庫上下文


protected override void OnModelCreating(ModelBuilder modelBuilder)

    {

        modelBuilder.Entity<EntityParent>()

            .HasKey(x => x.Id);


        modelBuilder.Entity<EntityChild>()

            .HasKey(x => x.Id);


        modelBuilder.Entity<ParentChildren>()

            .HasKey(x => new { x.ParentId , x.ChildId });


        modelBuilder.Entity<ParentChildren>()

            .HasOne(x => x.Parent)

            .WithMany(m => m.Childrens)

            .HasForeignKey(x => x.ParentId);


        modelBuilder.Entity<ParentChildren>()

            .HasOne(x => x.Child)

            .WithMany(e => e.TransformedParents)

            .HasForeignKey(x => x.ChildId);

    }


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 206 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號