Solved

.MapRightKey

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

911 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

22 Experts available now in Live!

Get 1:1 Help Now