I have two classes:
public class MultilingualString
{
public int Id { get; set; }
public string DefaultText { get; set; }
public IList<Translation> Translations { get; set; }
}
and
public class Translation
{
public string Language { get; set; }
public string Text { get; set; }
public MultilingualString Owner { get; set; }
}
Now, I’d like to have tables MultilingualStrings and Translations with the following schema:
MultilingualString
ID INT PK
DefaultText NVARCHAR
and
Translations
StringID PK, FK
Language NCHAR(5) PK
Text NVARCHAR
But don’t know how to create my code first model mapping with EF4/EF5. Is this scenario possible with EF?
It’s not possible to create a mapping between your tables and your entities because you must expose the full primary key as property/properties in your entity classes. Your table
Translationshas a composite primary key (StringID+Language) but your entityTranslationonly has a part of this key (Language) as a property.However, if you introduce the
StringIDas property inTranslationthe mapping should be possible:Mapping with Fluent API: