I have the following entity schema for a multilingual database:
[Table("Translation")]
public class Translation
{
public string Code { get; set; } = string.Empty;
public int LCID { get; set; }
public string Text { get; set; } = string.Empty;
}
Translations are all stored in one table with a composite primary key, the translation code and the language id (LCID). The Code is the same for all translations of the same text.
Other tables would reference to the possible translations by using the TranslationCode as a foreign key.
For example a Category table would look like:
[Table("Category")]
public class Category
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid Id { get; set; }
public string TranslationCode { get; set; } = string.Empty;
public HashSet<Translations.Translation> Translations { get; set; } = new HashSet<Translations.Translation>();
}
How is it possible to configure the above arrangement with code first fluent api? The following code obviously does not work…
builder.Entity<Translations.Translation>()
.HasKey(t => new { t.Code, t.LCID });
builder.Entity<Categories.Category>()
.HasKey(c => c.Id);
builder.Entity<Categories.Category>()
.HasMany(c => c.Translations)
.WithMany(); /// ??? What should I write here?
I could have a separate table to store TranslationCodes as single primary key and make it a foreign key in Translation table but that would be redundant and AFAIK this one is a valid arrangement. I would stick to it if it is possible to make EF to handle the mapping.
Any suggestion is appreciated. Thank you.