Solved

.MapRightKey

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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