Вот мой пример рабочего кода
Модели
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; }
}
В контексте класса БД
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; }
}
Контроллер
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);
}
}
Здесьэто вывод 