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

发布于 2024-11-06 11:22:02 字数 934 浏览 0 评论 0原文

我开始使用 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; }
    }
}

搭建这些模型后,我可以创建会话和演讲者,但在演讲者视图中,我无法选择任何会话,并且在会话视图中我无法选择任何演讲者。

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

提前致谢, 约西

I started using MVC 3 and used MvcScaffolding to scaffold 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 scaffolding these models, I can create sessions and speakers, but in the speakers view, I cannot choose any sessions, and in the sessions view I cannot choose any speakers.

How can I add these and make them mutliselect options, such that I can choose 10 speakers for one specific session, e.g.?

Thanks in advance,
Yosy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最好是你 2024-11-13 11:22:02

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

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

You need this in your context class: (this will create an association table)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Entity<Speaker>()
                 .HasMany(parent => parent.Session)
                 .WithMany();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文