Solved

Code First with Many to many relationship in MVC

Posted on 2013-11-23
2
387 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 63

Accepted Solution

by:
Fernando Soto earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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