Skip to content

Instantly share code, notes, and snippets.

@owen2
Last active April 7, 2017 12:16
Show Gist options
  • Save owen2/5dff481ad03e40161e4b7ad942f032ec to your computer and use it in GitHub Desktop.
Save owen2/5dff481ad03e40161e4b7ad942f032ec to your computer and use it in GitHub Desktop.
EF Code First Association Table
//From Model Builder
modelBuilder.Entity<ThisTable>().
HasMany(c => c.OtherTable).
WithMany(p => p.AssocTable).
Map(
m =>
{
m.MapLeftKey("ThisId");
m.MapRightKey("OtherId");
m.ToTable("AssocTable");
});
//From entity configuration class
HasMany(p => p.OtherTable).WithMany().Map(assoc =>
{
assoc.MapLeftKey("ThisId");
assoc.MapRightKey("OtherId");
assoc.ToTable("MakeUpANameForYourNewAssocTable");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment