3

I'm building an Entity Framework Core-backed ASP.NET Core RESTful service and I have entities / models such as Product, Document etc with description in multiple languages. I want to be able to dynamically query Products with relative Documents by providing their basic information with translated descriptions (based on current UI culture set by headers/cookies etc)

Unfortunately EF Core's Include called on navigation properties can't be filtered like this:

_dbContext.Products
   .Include(p => p.Documents,
          d => d.Language.Equals(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName));

I know EF Core 2.1 has global filters now, which can be used to manage soft delete or multi-tenancy etc, but I need per-request filters by language (if needed).

So I'm wondering how do I accomplish such functionality and if there is any best practice for managing translations in EF Core 2.1 in general (e.g. via an [Owned] property, a navigation property with some extension for conditional include etc)

Salaros
  • 131
  • 5

1 Answers1

3

As far as I can tell, there's no out of the box functionality for your use case.

Note however that you can always do a manual join:

var result = _dbContext
    .Products
    .Join(
        _dbContext
            .Documents
            .Where(d => d.Language.Equals(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)),
        product => product.ID,
        document => document.ProductID,
        (product, document) => new {Product = product, Document = document}
    );

And use a GroupBy or anything else you might need later on

gcali
  • 172
  • 2
  • 7