簡體   English   中英

我希望孫子實體上沒有父實體或外鍵

[英]I want no Parent entity or foreign key on the Grandchild entity

我有三個實體, BaseEntityChildGrandChild ,它們可以預見地鏈接在一起。

它是這樣的:

    public class BaseEntity
    {
        public string CompanyId { get; set; }

        public string BaseEntityId { get; set; }

        public virtual List<Child> Children { get; set; }
    }

    public class Child
    {
        public string CompanyId { get; set; }

        public string BaseEntityId { get; set; }

        public string ChildId { get; set; }

        public virtual BaseEntity Parent { get; set; }

        public virtual List<GrandChild> GrandChildren { get; set; }
    }

    public class GrandChild
    {
        public string CompanyId { get; set; }

        public string BaseEntityId { get; set; }

        public string ChildId { get; set; }

        public string GrandChildId { get; set; }

        public virtual Child ParentChild { get; set; }
    }

    public class BaseContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder p_mbuModel)
        {
            p_mbuModel.Entity<BaseEntity>().ToTable("T_BaseEntity");

            p_mbuModel.Entity<BaseEntity>().HasKey(t => new { t.CompanyId, t.BaseEntityId });

            p_mbuModel.Entity<Child>().ToTable("T_Child");

            p_mbuModel.Entity<Child>().HasKey(t => new { t.CompanyId, t.BaseEntityId, t.ChildId });

            p_mbuModel.Entity<Child>().HasOne(c => c.Parent).
                                       WithMany(p => p.Children).
                                       HasForeignKey(c => new { c.CompanyId, c.BaseEntityId }).
                                       HasConstraintName("FK_Child_BaseEntity");

            p_mbuModel.Entity<GrandChild>().ToTable("T_GrandChild");

            p_mbuModel.Entity<GrandChild>().HasKey(t => new { t.CompanyId, t.BaseEntityId, t.ChildId, t.GrandChildId });

            p_mbuModel.Entity<GrandChild>().HasOne(gc => gc.ParentChild).
                                            WithMany(c => c.GrandChildren).
                                            HasForeignKey(gc => new { gc.CompanyId, gc.BaseEntityId, gc.ChildId }).
                                            HasConstraintName("FK_GrandChild_Child");
        }
    }

你會注意到它沒有從GrandChildBaseEntity直接鏈接,也沒有GrandChildBaseEntity之間的外鍵。 這是一致的。 我不希望直接鏈接。 (如果沒有別的,它可能會導致級聯刪除的不愉快。)

不過,當我啟動Add-Migration時,我得到以下內容:

    table.ForeignKey(
        name: "FK_T_GrandChild_T_BaseEntity_BaseEntityId",
                        columns: x => { x.CompanyId, x.BaseEntityId },
                        principalTable: "T_BaseEntity",
                        principalColumns: new { "CompanyId", "BaseEntityId" },
                        onDelete: ReferentialAction.Cascade);

我試圖阻止的事情。 (在遷移時,它會創建我不想要的外鍵約束,並且使用那個笨拙的名稱來引導。)

我試過添加

    p_mbuModel.Entity<GrandChild>().Ignore(t => new { t.CompanyId, t.BaseEntityId });

這引起了一個例外

The expression 't => new <>f__AnonymousType10`2(CompanyId = t.CompanyId, BaseEntityId = t.BaseEntityId)' is not a valid property expression. The expression should represent a simple property access: 't => t.MyProperty'.

(我猜這是可以預料的;這對我來說是一個很長的鏡頭。)

我可以將GrandParent屬性添加到GrandChild並在其上使用Ignore() ,但這需要我創建我想要隱藏的鏈接。

我希望BaseEntityGrandChild之間沒有鏈接,既不是實體也不是數據庫表。

我怎樣才能做到這一點?

......我很尷尬承認這一點。 當我發布問題時,我修剪了課程中不相關的部分。 我沒有注意到BaseEntity包含這一行:

        public virtual List<GrandChild> GrandChildren { get; set; }

我刪除了它並再次啟動了Add-Migration。 BaseEntityGrandChild之間沒有出現任何鏈接。

向任何試圖理解這個不完整的謎語的人道歉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM