Using EF Core ThenInclude() on Junction tables

后端 未结 1 826
你的背包
你的背包 2020-12-03 13:38

I\'m transfering my .NET Framework (EF6) code to ASP.NET Core (EF Core), and I stumbled upon this issue. Here is some example code:

In EF6 I use Include() and Select

相关标签:
1条回答
  • 2020-12-03 13:50

    but I get an ICollection in the last lambda displayed, so I obviously need the Select()

    No, you don't. EF Core Include / ThenInclude totally replace the need of Select / SelectMany used in EF6. Both they have separate overloads for collection and reference type navigation properties. If you use the overload with collection, ThenInclude operates on the type of the collection element, so at the end you always end up with a single entity type.

    In your case, pa should resolve to your junction table element type, so Author should be directly accessible.

    For instance the EF6 include chain:

    .Include(p => p.PostAuthor.Select(pa => pa.Author).Select(a => a.Interests))
    

    translates to EF Core:

    .Include(p => p.PostAuthor).ThenInclude(pa => pa.Author).ThenInclude(a => a.Interests)
    
    0 讨论(0)
提交回复
热议问题