简体   繁体   中英

EF Core 2.0 does not load related entities when using where condition

I have an entity called Category that has a recursive relationship like this:

public class Category
    {
        public int CategoryId { get; set; }
        public int? ParentCategoryId { get; set; }
        public string Description { get; set; }

        public Category ParentCategory { get; set; }
        public ICollection<Category> Children { get; set; }
        public ICollection<ProductCategory> Products { get; set; }

        public Category()
        {
            Products = new List<ProductCategory>();
            Children = new List<Category>();
        }
    }

I'm having trouble understanding why this code

return await Context.Set<Category>()
                .Where(c => c.CategoryId == id)
                .OrderBy(c => c.Description)
                .ToListAsync();

is returning only the filtered category without its children and this query:

return await Context.Set<Category>()
                .OrderBy(c => c.Description)
                .ToListAsync();

is returning all the categories with the related children of each one. If I add an .Include(c => c.Children) to the filtered results, it returns what i'm looking for.

The main thing is that I want to understand why are those queries returning different result in terms of the children categories.

The first query:

return await Context.Set<Category>()
            .Where(c => c.CategoryId == id)
            .OrderBy(c => c.Description)
            .ToListAsync();

Is not returning Children because you are only loading the Category that matches the id provided from the database. If you want the children you need to Include(c => c.Children)

The second query:

return await Context.Set<Category>()
            .OrderBy(c => c.Description)
            .ToListAsync();

Has no filter, so EF Core is loading all Categories from the database. This contains the children because those entities get loaded as "parent" objects as well, so Entity will map the Navigation property using foreign keys since the entities are already being tracked in the Context.

As an example, if you did this:

Category parentCategory = await Context.Set<Category>()
        .SingleOrDefaultAsync(c => c.CategoryId == id);

List<Category> childCategories = await Context.Set<Category>()
        .Where(c => c.ParentCategoryId == parentCategory.CategoryId)
        .ToListAsync();

Then parentCategory would have its Children property populated, even though you didn't Include() them, but because the Entities were all being tracked within the scope of the same DbContext the relationship is mapped. In the above code, if you put a breakpoint on the second statement and inspected parentCategory , you would see that its Children property has no items until after you execute the second statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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