ff8mania
ff8mania

Reputation: 1760

EF Core include list of list

I have a very simple model. the main class, Recipe, contains a list of RecipeItem. Every RecipeItem has a list of RecipeItemComponents.

Using the Entity Framework context, I can do this:

var ret = await _context.Recipes
.Include(x => x.RecipeItems)
.ToListAsync();

This code returns The recipes with the RecipeItems, but for every RecipeItems I have not RecipeItemsComponent list. It makes sense since I'm not including those, but I'm, not sure no how to do it.

Thanks

Upvotes: 5

Views: 10490

Answers (2)

Rudresha Parameshappa
Rudresha Parameshappa

Reputation: 3926

Here is my working code sample

Models

public class Parent
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual List<Child1> Child1s { get; set; }
}

 public class Child1
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int ParentId { get; set; }
    public Parent Parent { get; set; }

    public virtual List<Child2> Child2s { get; set; }
}

public class Child2
{
    public int Id { get; set; }

    public string Name { get; set; }

    public int Child1Id { get; set; }
    public Child1 Child1 { get; set; }
}

In the DB context class

public class TestDbContext : DbContext
{
    public TestDbContext(DbContextOptions<TestDbContext> options)
        : base(options)
    {
        Database.EnsureCreated();
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parent>().HasMany(x => x.Child1s).WithOne(x => x.Parent).HasForeignKey(x => x.ParentId);
        modelBuilder.Entity<Child1>().HasMany(x => x.Child2s).WithOne(x => x.Child1).HasForeignKey(x => x.Child1Id);

        this.InitialData(modelBuilder);
        base.OnModelCreating(modelBuilder);
    }

    protected void InitialData(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Parent>().HasData(new Parent[]
        {
            new Parent
            {
                Id = 1,
                Name = "Parent 1",
            }
        });

        modelBuilder.Entity<Child1>().HasData(new Child1[]
        {
            new Child1
            {
                Id = 1,
                Name = "Child 1 1",
                ParentId = 1,
            }
        });
        modelBuilder.Entity<Child2>().HasData(new Child2[]
        {
            new Child2
            {
                Id = 1,
                Name = "Child 2 1",
                Child1Id = 1
            }
        });
    }

    public DbSet<Parent> Parent { get; set; }
    public DbSet<Child1> Child1s { get; set; }
    public DbSet<Child2> Child2s { get; set; }
}

Controller

public class ParentsController : Controller
{
    private readonly TestDbContext _context;

    public ParentsController(TestDbContext context)
    {
        _context = context;
    }        public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var parent = await _context.Parent
            .Include(x=>x.Child1s).ThenInclude(x=>x.Child2s)
            .FirstOrDefaultAsync(m => m.Id == id);
        if (parent == null)
        {
            return NotFound();
        }

        return View(parent);
    }
}

Here is the output enter image description here

Upvotes: 6

jle
jle

Reputation: 9489

You can't use the strongly typed extension methods to include everything. In some cases you need to use a string:

.Include("RecipeItems.RecipeItemsComponents")

For those curious, the documentation for this overload is here: https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.entityframeworkqueryableextensions.include?view=efcore-2.1#Microsoft_EntityFrameworkCore_EntityFrameworkQueryableExtensions_Include__1_System_Linq_IQueryable___0__System_String_

Upvotes: -2

Related Questions