EF Core include list of list

.net c# entity-framework-core

Question

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

1
0
4/23/2019 1:26:55 PM

Popular Answer

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

0
4/23/2019 2:23:39 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow