简体   繁体   English

实体框架代码优先加载父子项 - 子项始终为空

[英]Entity Framework Code-First Eager Load parent child - children always empty

This is my first code-first project and have never had an issue doing this in db-first, that I can remember.这是我的第一个代码优先项目,在我记得的 db-first 中从来没有遇到过这个问题。 So, I'm either missing something obvious or there's a rule I missed somewhere.所以,我要么遗漏了一些明显的东西,要么我在某处遗漏了一条规则。 I do not want to use lazy loading, and have to think that it's not required to do this, given the many examples using eager loading.我不想使用延迟加载,并且考虑到使用预先加载的许多示例,我不得不认为不需要这样做。

Very simple.很简单。 Parent record is Listing, child records are Bid entities:父记录为 Listing,子记录为 Bid 实体:

public class Listing {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ListingID { get; set; }

    [Required]
    [StringLength(140)]
    public string Title { get; set; }

    //...etc.

    public List<Bid> Bids { get; set; }

    public Listing() {
        Bids = new List<Bid>();
    }
}

public class Bid {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int BidID { get; set; }

    [Required]
    public decimal Amount { get; set; }

    //...etc.

    public int ListingID { get; set; }

    [ForeignKey("ListingID")]
    public Listing Listing { get; set; }

    public Bid() {
        Listing = new Listing();
    }
}

I'm using Include to pull the child records:我正在使用 Include 来拉取子记录:

        using (var db = new MyDbContext()) {
            var listing = db.Listings
                .Where(x => x.Active == true && x.UserID == "abc123")
                .Include(x => x.Bids)
                .FirstOrDefault();
        }

Child collection exists but is always empty (Count = 0) - it's not pulling the records.子集合存在但始终为空(计数 = 0) - 它不会提取记录。 They're definitely in the tables.他们肯定在桌子上。 I can manually query this and get those bid records, no problem.我可以手动查询并获取那些投标记录,没问题。 Basically, I need a listing and all of its bids, similar to this:基本上,我需要一个列表及其所有出价,类似于:

select l.*, b.*
from Listing l
inner join Bid b on l.ListingID = b.ListingID
where l.Active = 1
and l.UserID = 'abc123'

What's missing?缺少了什么? This article says I'm doing it right:这篇文章说我做对了:

https://msdn.microsoft.com/en-us/data/jj574232.aspx https://msdn.microsoft.com/en-us/data/jj574232.aspx

Figured it out.弄清楚了。 I KNEW I had done this type of relationship before.我知道我以前做过这种类型的关系。 I found an older project where I did the exact same thing.我找到了一个旧项目,在那里我做了完全相同的事情。 All I had to do was remove the constructor in the Bid class.我所要做的就是删除 Bid 类中的构造函数。 Apparently it goes awry if you specify both sides of the relationship?如果您指定关系的双方,显然会出错? I don't know...but now it works and it's pulling the expected records.我不知道......但现在它可以工作并且正在拉取预期的记录。 Frustrating!令人沮丧! Seems like they could add some validation to help you from shooting yourself in the foot, so easily.似乎他们可以添加一些验证来帮助您轻松地用脚射击自己。 If it's not valid, it'd be nice to know for certain.如果它无效,那么肯定会很高兴。

Everything the same as the OP, save for this class:一切都与 OP 相同,除了这个类:

public class Bid {
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int BidID { get; set; }

    [Required]
    public decimal Amount { get; set; }

    //...etc.

    public int ListingID { get; set; }

    [ForeignKey("ListingID")]
    public Listing Listing { get; set; }

    //public Bid() {
    //    Listing = new Listing();
    //}
}

尝试删除两个类的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM