How to query many-to-many releationship in EF Core

asp.net-core asp.net-mvc c# entity-framework entity-framework-core

Accepted Answer

You'll need to manually do this since EF Core won't load associated properties automatically, but something like the following should work:

var result = context.Begrip
    .Include(x => x.Categories)
    .ThenInclude(x => x.category);

Note that Intellisense may not always function on.ThenInclude at the present, but even if it receives a red underlining, the code should still compile.

You should probably map this to a DTO if you're returning this to the view or an API so you don't have to worry about.Categories[0].category.Name etc.

25
6/12/2017 1:37:36 PM

Popular Answer

ZZZ_tmp






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