MvcScaffolding: How to Maintain Many-to-Many Relationships Between Objects

I started using MVC 3 and used MvcScaffolding to create these models:

namespace Conference.Models { /* * Speaker can have many session * And session can have many speakers */ public class Speaker { public Guid Id { get; set; } [Required] public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Session> Sessions { get; set; } } public class Session { public Guid Id { get; set; } [Required] public string Title { get; set; } [Required] public string Description { get; set; } [Required] public DateTime Hour { get; set; } public virtual ICollection<Speaker> Speakers { get; set; } } } 

After creating these models, I can create sessions and speakers, but in the speaker view, I cannot select any sessions, and in the viewing session I can not select speakers.

How can I add these parameters and make them mutliselect options so that I can select 10 speakers for one specific session, for example?

Thanks in advance, Yoshi

+7
source share
1 answer

This is needed in your class context: (this will create an association table)

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Speaker>() .HasMany(parent => parent.Session) .WithMany(); } 
+1
source

All Articles