[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Code First with Many to many relationship in MVC

Posted on 2013-11-23
2
Medium Priority
?
400 Views
Last Modified: 2013-11-23
I'm trying to understand the following code bellow. Maybe someone can help, thank you...

 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"));
      }

Open in new window


This was from this tutorial...
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-a-more-complex-data-model-for-an-asp-net-mvc-application
0
Comment
Question by:yguyon28
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Author Comment

by:yguyon28
ID: 39671493
Not sure what these really mean....

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

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 39671570
Hi yguyon28;

This function is called the first time you attempt to call Entity Framework queries to make any changes to the default configuration.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // By default Entity Framework pluralizes table names in your model so
    // that a table in your database for example may be called Instructor will
    // be called in your modle as Instructors. This statement does not allow 
    // this to happen. The other statements in this function can infact change 
    // this to take on different names if so configured.
   modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

   // This is the pure join table that does not show up in your model but will be
   // created in the database. This configuration is not needed and Entity Framework
   // will use default names for Key's and table names, for example the InstructorID 
   // key field in this pure join table will have been called InstructorInstructorId,
   // but because of this connfiguration command it will be called InstructorID.
   //
   // So looking at this configuration command it states that the Entity Course in 
   // the modle has many Instructor's using the navigation property Instructors and 
   // that the Entity Instructor has many Course useing the navigation property 
   // Courses. The pure join table will have the first column name as CourseID, 
   // given by MapLeftKey, and a second column as InstructorID, as indicated by 
   // MapRightKey, and the table name of the pure join table is CourseInstructor.
   modelBuilder.Entity<Course>()
       .HasMany(c => c.Instructors).WithMany(i => i.Courses)
       .Map(t => t.MapLeftKey("CourseID")
           .MapRightKey("InstructorID")
           .ToTable("CourseInstructor"));
}

Open in new window

0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 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