简体   繁体   English

使用Entity Framework 7填充多对多对象

[英]Getting many-to-many objects populated with Entity Framework 7

I have the following Many-To-Many relationship setup illustrated here by a database diagram: 我有以下数据库图说明的多对多关系设置: 在此处输入图片说明

These are represented by the following POCO classes, from which the database was created using Entity Framework code first: 这些由以下POCO类表示,首先使用Entity Framework代码从中创建数据库:

public class ExerciseCategory
{
    public int ExerciseId { get; set; }
    public Exercise Exercise { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

public class Exercise
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<ExerciseCategory> ExerciseCategories  { get; set; }
}

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<ExerciseCategory> ExerciseCategories { get; set; }
}

Now I need to get all exercises which would include the list of categories they belong to. 现在,我需要进行所有练习,其中包括它们所属的类别列表。 Since it is a many-to-many relationship one exercise can have many categories. 由于这是多对多关系,因此一个练习可以具有许多类别。 I tried the following code: 我尝试了以下代码:

_context.Exercises
            .Include(e => e.ExerciseCategories)
            .OrderBy(e => e.Name).ToList();

But it does not populate the properties of ExerciseCategory... 但是它不会填充ExerciseCategory的属性...

How should I construct my query to get ExerciseCategory's Category property populated? 我应该如何构造查询以获取ExerciseCategory的Category属性? Preferable in one query and not within a "for" loop, because potentially there will be many exercises displayed on one page and the performance might be an issue. 最好在一个查询中而不是在“ for”循环中,因为可能在一页上显示很多练习,而性能可能是一个问题。

Entity Framework 7 many-to-many support is still not complete. 实体框架7多对多支持仍未完成。 Currently you have to map one-to-many relationships manually for your Exercise and Category entities. 当前,您必须为“ Exercise和“ Category实体手动映射一对多关系。 Your entity classes are set up correctly. 您的实体类别已正确设置。 You now need to override OnModelCreating method in your DbContext class: 现在,您需要在DbContext类中重写OnModelCreating方法:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<ExerciseCategory>()
        .HasKey(t => new { t.ExerciseId, t.CategoryId });

    modelBuilder.Entity<ExerciseCategory>()
        .HasOne(pt => pt.Exercise)
        .WithMany(p => p.ExerciseCategories)
        .HasForeignKey(pt => pt.ExerciseId);

    modelBuilder.Entity<ExerciseCategory>()
        .HasOne(pt => pt.Category)
        .WithMany(t => t.ExerciseCategories)
        .HasForeignKey(pt => pt.CategoryId);
}

See Relationships - Entity Framework 7.0 Documentation 请参阅关系-实体框架7.0文档

Also make sure to include DbSet<ExerciseCategory> property in you context. 还要确保在上下文中包含DbSet<ExerciseCategory>属性。

After this the complete eager query can be constructed in following way: 之后,可以按以下方式构建完整的热切查询:

_context.ExerciseCategories.Include(ec => ec.Exercise)
                    .Include(ec => ec.Category)
                    .Select(ec => ec.Exercise)

Or by using ThenInclude on Exercise-query: 或通过在Exercise-query上使用ThenInclude:

_context.Exercises.Include(e => e.ExerciseCategories)
                  .ThenInclude(ec => ec.Category)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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