EF Code first parent child mapping

ef-code-first, entity-framework

Solution

The first part of your mapping can be done this way:

modelBuilder.Entity<File>()
    .HasMany(f => f.FileLocalCollection)
    .WithRequired()
    .HasForeignKey(fl => fl.FileId);

modelBuilder.Entity<FileLocal>()
    .HasKey(fl => new {fl.FileId, fl.LangId});

And the second part depends on the way how your `Lang` is defined. For example if you add navigation property from `FileLocal` to `Lang` you can map it this way:

modelBuilder.Entity<FileLocal>()
    .HasRequired(fl => fl.Lang)
    .WithMany()
    .HasForeignKey(fl => fl.LangId);

Problem

This may be a duplicate qn, but i couldnt get a proper answer to this scenario. I have the following table structure: ``` public class File { public int FileId { get; set; } //PK public int VersionID { get; set; } public virtual ICollection<FileLocal> FileLLocalCollection { get; set; } } public class FileLocal { public int FileId { get; set; } //PK, FK public int LangID { get; set; } //PK,FK public string FileName { get; set; } } ``` I have not included the third table here(Its basically LangID (PK) & LangCode ) How do i specify this mapping in fluent Api so that i can load "FileLLocalCollection" with every File objects?

Original source