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)

asked Jul 9, 2018 at 12:14

1 Answer 1

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

answered Jul 9, 2018 at 17:02
1
  • 2
    This is kinda what I thought; if Include doesn't do it for you, ratchet down to something a bit more fundamental. Commented Jul 10, 2018 at 1:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.