EF Core包含清单清单

时间:2019-04-23 13:26:55

标签: c# .net entity-framework-core

我有一个非常简单的模型。 主类Recipe包含RecipeItem列表。每个RecipeItem都有一个RecipeItemComponents列表。

使用Entity Framework上下文,我可以这样做:

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

此代码返回带有RecipeItems的配方,但是对于每个RecipeItems我都没有RecipeItemsComponent列表。 这是有道理的,因为我没有包括这些,但我不确定是否要这样做。

谢谢

2 个答案:

答案 0 :(得分:0)

这是我的工作代码示例

模型

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);
    }
}

这是输出 enter image description here

答案 1 :(得分:-1)

您不能使用强类型扩展方法来包含所有内容。在某些情况下,您需要使用字符串:

Application.DisplayAlerts = False
wkb1.Close True
Application.DisplayAlerts = True

对于那些好奇的人,此过载的文档在这里: https://docs.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_