MvcScaffolding:如何支持实体之间的多对多关系

Yos*_*osi 7 scaffolding entity-framework-4 ef-code-first asp.net-mvc-3

我开始使用MVC 3并使用MvcScaffolding来构建这些模型:

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

在脚手架这些模型之后,我可以创建会话和扬声器,但在扬声器视图中,我无法选择任何会话,并且在会话视图中我无法选择任何扬声器.

我如何添加这些并使它们成为mutliselect选项,这样我可以为一个特定的会话选择10个发言者,例如?

先谢谢你,Yosy

Cha*_*ngh 1

您的上下文类中需要这个:(这将创建一个关联表)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Speaker>()
                 .HasMany(parent => parent.Session)
                 .WithMany();
}
Run Code Online (Sandbox Code Playgroud)