Solved

Code First with Many to many relationship in MVC

Posted on 2013-11-23
2
379 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
ID: 39671493
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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

773 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