gpt4 book ai didi

asp.net-mvc - MVC 5 脚手架不能为多对多关系添加选择列表吗? (添加脚手架 - 带有 View 的 MVC 5 Controller ,使用 Entity Framework )

转载 作者:行者123 更新时间:2023-12-01 09:27:54 26 4
gpt4 key购买 nike

我希望能够使用 Visual Studio 2013 中的 "Add - Scaffold" 为与另一个模型具有多对多关系的模型添加 CRUD。不幸的是,脚手架 View / Controller 根本没有触及关系,在创建/编辑 View 中没有呈现 SelectList。

Scaffold 虽然适用于一对多关系。多对多是 Scaffold 工具中未实现的功能,还是我做错了什么?

我正在使用 Fluent API。

这些是我的模型(为了便于阅读,去掉了)

public class Category
{
public int Id { get; set; }
public virtual ICollection<Country> Countries { get; set; }
}

public class Country
{
public string Iso { get; set; }

public string GlobalName { get; set; }
public string LocalName { get; set; }

public virtual ICollection<Category> Categories { get; set; }
}

这些是 Fluent API 配置类

public class CategoryConfiguration: EntityTypeConfiguration<Category>
{
public CategoryConfiguration()
{
HasKey(c => new { c.Id });

HasMany(c => c.Countries)
.WithMany(c => c.Categories)
.Map(m =>
{
m.ToTable("CategoryCountry_JT");
m.MapLeftKey("CategoryId");
m.MapRightKey("CountryId");
});
}
}

public class CountryConfiguration : EntityTypeConfiguration<Country>
{
public CountryConfiguration()
{
HasKey(c => new { c.Iso });

Property(c => c.GlobalName).IsRequired();
Property(c => c.LocalName).IsRequired();
}
}

也许连接表必须声明为真实模型,而不是通过 .Map(m => ... 才能使 Scaffold 工作?

最佳答案

显然内置的 Scaffold 功能不支持多对多关系。

有一个pretty good blog post about it here on MSDN ,它包含一个可用的解决方案。

关于asp.net-mvc - MVC 5 脚手架不能为多对多关系添加选择列表吗? (添加脚手架 - 带有 View 的 MVC 5 Controller ,使用 Entity Framework ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19973176/

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