当我为控制器创建Scaffold并添加Model类时,出现这些错误“不支持每种类型的多个对象集”。
我有三个Model类:
1.部门
2.Designation.cs
3.CompanyDBContext.cs
数据库:数据库中有两个表,1.部门(deptID,deptName,描述)2.名称(desgtID,desgName,描述)
目标:-我想为这些情况创建一个视图页面。像这样
插入表格名称(文本框)+部门名称(下拉列表框)+名称(下拉列表框)
1.部门
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
并且运行时不知道(并且无法表达它),所以这就是您得到错误的原因。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句