Solved

.MapRightKey

Posted on 2014-02-23
2
177 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 62

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This video discusses moving either the default database or any database to a new volume.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now