Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instruct ef core to inject some runtime value when constructing entity #24335

Closed
mhDuke opened this issue Mar 7, 2021 · 1 comment
Closed

Comments

@mhDuke
Copy link

mhDuke commented Mar 7, 2021

I must shout out to ef core team, and arthur. you guys doing splendid job.
I am experimenting to implement internationalization with the help of ef core. currently each entity has a List of another object which holds the translatable properties. example code

public class Language
{
    public int Id { get; set; }
    public int Name { get; set; }
}
public class Employe
{
    private readonly Language _language;
    public Employe(string name, double salary, Language language)
    {
        Translations = new List<EmployeTranslation>();
        Salary = salary;
        UpdateName(name, language);
    }
    private Employe(Language language)
    {
        _language = language;
    }

    public int Id { get; set; }
    public double Salary { get; set; }
    public string Name => Translations.FirstOrDefault(e => e.Language.Equals(_language))?.Name;
    public ICollection<EmployeTranslation> Translations { get; set; }

    public void UpdateName(string newName, Language language)
    {
        var existingLanguageTranslation = Translations.FirstOrDefault(e => e.Language.Equals(language));
        if (!(existingLanguageTranslation is null))
            Translations.Remove(existingLanguageTranslation);

        Translations.Add(new EmployeTranslation(language, newName));
    }
}
public class EmployeTranslation
{
    public EmployeTranslation(Language language, string name)
    {
        Language = language;
        Name = name;
    }
    public Language Language { get; set; }
    public string Name { get; set; }
}

I would like ef core to inject a runtime Language object or even an int LanguageId when constructing several entity types via the private constructor. in Entity types with constructors documentation it clearly says

Currently, only services known by EF Core can be injected. Support for injecting application services is being considered for a future release.

but that was 3 years ago. is it possible for ef core 3.1? or even ef core 5?

currently im using a global filter on Translation entities to filter for a current request language as a workaround. or perhaps as long lasting solution.

@AndriySvyryd
Copy link
Member

Duplicate of #10789

@AndriySvyryd AndriySvyryd marked this as a duplicate of #10789 Mar 8, 2021
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants