Solved

Code First with Many to many relationship in MVC

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…

828 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