Solved

.MapRightKey

Posted on 2014-02-23
2
183 Views
Last Modified: 2014-03-03
Trying to understand what the following code mean.... MapLeftKey???

Map(t => t.MapLeftKey("CourseID")
                 .MapRightKey("InstructorID")
                 .ToTable("CourseInstructor"));


using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace ContosoUniversity.DAL
{
   public class SchoolContext : DbContext
   {
      public DbSet<Course> Courses { get; set; }
      public DbSet<Department> Departments { get; set; }
      public DbSet<Enrollment> Enrollments { get; set; }
      public DbSet<Instructor> Instructors { get; set; }
      public DbSet<Student> Students { get; set; }
      public DbSet<OfficeAssignment> OfficeAssignments { get; set; }

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
         modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

         modelBuilder.Entity<Course>()
             .HasMany(c => c.Instructors).WithMany(i => i.Courses)
             .Map(t => t.MapLeftKey("CourseID")
                 .MapRightKey("InstructorID")
                 .ToTable("CourseInstructor"));
      }
   }
}
0
Comment
Question by:yguyon28
2 Comments
 

Author Comment

by:yguyon28
ID: 39881239
modelBuilder.Entity<Course>()
             .HasMany(c => c.Instructors).WithMany(i => i.Courses)
             .Map(t => t.MapLeftKey("CourseID")
                 .MapRightKey("InstructorID")
                 .ToTable("CourseInstructor"));

Does this actually create a table CourseInstructor?
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39881253
Hi yguyon28;

This is building a many-to-many relationship from the Course table, the LeftKey, to a juction table called CourseInstructor, created in the database only, to the Instructor table, the RightKey side.

modelBuilder
    // Defines the table we are defining
    .Entity<Course>()
    // Course has a collection called Instructors and that Instructor has a collection of Courses.
    .HasMany(c => c.Instructors).WithMany(i => i.Courses)
    // The next three statements define the Juction table
    // The first entry in the table is the primary key from Course table identified as MapLeftKey
    .Map(t => t.MapLeftKey("CourseID")
    // The second entry in the table is the primary key from Instructor table identified as MapRightKey    
    .MapRightKey("InstructorID")
    // This is the table name for the junction table
    .ToTable("CourseInstructor"));
  

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Run time Error 4 49
SQL Login 17 51
Install Problem 13 32
Jquery/JSON passing value from one function to another 12 41
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question