简体   繁体   中英

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:

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...

How should I construct my query to get ExerciseCategory's Category property populated? 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.

Entity Framework 7 many-to-many support is still not complete. Currently you have to map one-to-many relationships manually for your Exercise and Category entities. Your entity classes are set up correctly. You now need to override OnModelCreating method in your DbContext class:

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

Also make sure to include DbSet<ExerciseCategory> property in you context.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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