gpt4 book ai didi

asp.net-mvc-3 - Mvc脚手架 : How to support many-to-many relationships between entities

转载 作者:行者123 更新时间:2023-12-04 12:56:10 24 4
gpt4 key购买 nike

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

搭建好这些模型后,我可以创建 session 和演讲者,但在演讲者 View 中,我无法选择任何 session ,在 session View 中我也无法选择任何演讲者。

我如何添加这些并使它们成为多选选项,以便我可以为一个特定 session 选择 10 个扬声器,例如?

提前致谢,
优西

最佳答案

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

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

关于asp.net-mvc-3 - Mvc脚手架 : How to support many-to-many relationships between entities,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5930438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com