当我为 Controller 创建脚手架并添加模型类时,我收到这些错误“不支持每种类型的多个对象集”。
我有三个模型类:
1.部门.CS
2.Designation.cs
3.CompanyDBContext.cs
数据库:我在数据库中有两个表,1. Department(deptID,deptName,Description) 2. Designation(desgtID,desgName,description)
目标:- 我想为这些场景创建一个 View 页面。像这样
插入表格名称(文本框)+ 部门名称(下拉列表框)+ 名称(下拉列表框)
1.部门.CS
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
}
2.Designation.cs
namespace mvcAppraisalSystem.Models
{
public class Designation
{
[Key]
public int desgID { get; set; }
public string desgName { get; set; }
public string description { get; set; }
}
}
3.CompanyDBContext.cs
namespace mvcAppraisalSystem.Models
{
public class CompanyDBContext : DbContext
{
public CompanyDBContext() : base("CompanyDBContext")
{
}
public DbSet<CompanyDBContext> Departments { get; set; }
public DbSet<CompanyDBContext> Designations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
}
最佳答案
您正在将您的集合创建为 DbSet<CompanyDBContext>
.你要的是DbSet<Department>
和 DbSet<Designation>
.
public DbSet<Department> Departments { get; set; }
public DbSet<Designation> Designations { get; set; }
这看起来很明显是一个拼写错误,但您收到错误的原因是因为运行时不知道如何在具有相同元素类型的相同上下文中填充多个对象集。这更像是在说:
public DbSet<Department> SomeDepartments { get; set; }
public DbSet<Department> OtherDepartments { get; set; }
因为(大概)你会期望一些东西来定义 SomeDepartments
中的内容并且会有一些东西来定义 OtherDepartments
中的内容并且运行时不知道这一点(也无法表达它),这就是您收到错误的原因。
关于c# - 不支持每种类型的多个对象集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955622/