簡體   English   中英

具有EF6子集合的父/子始終為null

[英]Parent/Child with EF6 child collection always null

每當我通過EF拉MyList對象時,父對象就正確關聯了,而Children集合始終為null。 不知道我在做什么錯,幾乎每篇文章都表明這樣做。

數據庫

CREATE TABLE [dbo].[MyList] (
    [MyListId]     BIGINT           IDENTITY (1, 1) NOT NULL,
    [ParentMyListId] BIGINT NULL, 
    CONSTRAINT [PK_MyList] PRIMARY KEY CLUSTERED ([MyListId] ASC) WITH (FILLFACTOR = 90),
    CONSTRAINT [FK_MyList_MyList_MyListId] FOREIGN KEY (ParentMyListId) REFERENCES MyList(MyListId)
);

模型

public class MyList
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long MyListId { get; set; }
    public long? ParentMyListId { get; set; }

    [ForeignKey("ParentMyListId")]
    public virtual List MyListParent { get; set; }

    public virtual ICollection<MyList> MyListChildren { get; set; }
}

數據庫上下文

public class MyContext : DbContext
{
    public MyContext() : base(Properties.Settings.Default.DbContext)
    {
        Configuration.LazyLoadingEnabled = false;
    }

    public DbSet<MyList> MyLists { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyList>()
        .ToTable("MyList", "dbo")
        .HasOptional(x => x.MyListParent)
        .WithMany(x => x.MyListChildren)
        .HasForeignKey(x => x.ParentMyListId)
        .WillCascadeOnDelete(false);
    }

    base.OnModelCreating(modelBuilder);
}

我在EF 6.1.3版本中嘗試了相同的結構,它的工作原理很吸引人。 我添加了輸出圖像和db中存在的數據。 如果禁用配置中的加載,唯一可能會停止工作的事情。 希望它對您有用,請嘗試我的示例代碼。

  // Your entity class I added name property to show you the results 
   public class MyList
    {
        [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long MyListId { get; set; }
        public long? ParentMyListId { get; set; }

        public string Name { get; set; }

        [ForeignKey("ParentMyListId")]
        public virtual MyList MyListParent { get; set; }

        public virtual ICollection<MyList> MyListChildren { get; set; }
    }       

     // DBContext please note no configuration properties set just default constructor 
     // you need t check here if you have set soemthing here 
       public class TestContext : DbContext
        {
            public TestContext()
                : base("name=TestConnection")
            {
            }               

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<MyList>()
                .ToTable("MyList", "dbo")
                .HasOptional(x => x.MyListParent)
                .WithMany(x => x.MyListChildren)
                .HasForeignKey(x => x.ParentMyListId)
                .WillCascadeOnDelete(false);

            }

            public virtual DbSet<MyList> Lists { get; set; }
        }

控制台應用程序顯示結果:

    static void Main(string[] args)
    {
        using (var ctx = new TestContext())
        {

            // for testing to see al working 
            //this is important to read the entity first .
            var parent = ctx.Lists.ToList();

            foreach (var p in parent)
            {
                foreach (var child in p.MyListChildren)
                {
                    Console.WriteLine(string.Format(@"Parent Name {0}  has child with name {1}", p.Name, child.Name));
                }
            }

        }

        Console.ReadLine();
    }

在數據庫中輸出應用程序和數據...

在此處輸入圖片說明

暫無
暫無

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

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