Solved

Code First with Many to many relationship in MVC

Posted on 2013-11-23
2
360 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
2 Comments
 

Author Comment

by:yguyon28
Comment Utility
Not sure what these really mean....

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

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

762 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

6 Experts available now in Live!

Get 1:1 Help Now