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

How to setup the DbContext to work with multi-language value tables? #23172

Closed
VitaliiIsaenko opened this issue Nov 2, 2020 · 1 comment
Closed
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@VitaliiIsaenko
Copy link

VitaliiIsaenko commented Nov 2, 2020

I got a problem with mapping my relation schema with multi-language support with EF Core ORM and desperately looking for help. I have an Event table (int id, text name, int typeId) and EventTypeLocale table (int id, text locale, text name).

To explain why I cannot restructure my db schema to attach EventId to EventTypeLocale table - there is an example content in EventTypeLocale:

(1,'en', 'External event')
(1,'de','Externe Veranstaltung')
(2,'en', 'Internal event ')
(2,'de','Internes Ereignis')

And Event table has info about session + its type - typeId that should reference EventTypeLocale with FK. But, as you can see from the example data - there will be several database rows by one id (since there is a composite key id + locale). I want to load the type in all locates into the EventTypeLocales collection.

I have two classes

public class Event
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int TypeId { get; set; }
    public List<EventTypeLocale> EventTypeLocales { get; set; }
}
public class EventTypeLocale
{
    public int Id { get; set; }
    public string Locale { get; set; }
    public string Name { get; set; }
}

The complex part is to configure it in the Context : DbContext class. Could you help me to configure such a relation? Is there such a possibility with EF Core? Or how should I change the model/(db schema but less desirable) to achieve what I want?

The question is also posted on SO and I asked another question a year ago where I got a working solution for fetching, but it restricts updating the FK field (typeId) because it is set up as a principal key. The "working" solution for fetching is below:

modelBuilder.Entity<Event>().ToTable("Event");
modelBuilder.Entity<EventTypeLocale>().ToTable("EventTypeLocale")
            .HasKey(p => new {p.Id, p.Locale});
modelBuilder.Entity<Event>()
            .HasMany(e=>e.EventTypeLocales)
            .WithOne()
            .HasForeignKey(x=>x.Id)
            .HasPrincipalKey(x=>x.TypeId);

Thank you in advance, I hope you have an idea on how to configure such a relation 🙏

@VitaliiIsaenko VitaliiIsaenko changed the title Would this multi-language db schema be possible to setup in EF Core 5? How to setup the DbContext to work with multi-language value tables Nov 2, 2020
@VitaliiIsaenko VitaliiIsaenko changed the title How to setup the DbContext to work with multi-language value tables How to setup the DbContext to work with multi-language value tables? Nov 2, 2020
@ajcvickers
Copy link
Contributor

@VitaliiIsaenko That mapping looks correct and I can't think of a way of doing this that doesn't make TypeId an alternate key. Therefore, this is probably a duplicate of #4073

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Nov 10, 2020
@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
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants