2

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)

2

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

  • 1
    This is kinda what I thought; if Include doesn't do it for you, ratchet down to something a bit more fundamental. – Robert Harvey Jul 10 '18 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.