C# 不支持每种类型的多个对象集?
当我为Controller创建Scaffold并添加Model类时,我得到了以下错误“不支持每种类型的多个对象集” 我有三个模型课: 1.c.部门C# 不支持每种类型的多个对象集?,c#,asp.net,visual-studio-2013,asp.net-mvc-5,C#,Asp.net,Visual Studio 2013,Asp.net Mvc 5,当我为Controller创建Scaffold并添加Model类时,我得到了以下错误“不支持每种类型的多个对象集” 我有三个模型课: 1.c.部门 namespace mvcAppraisalSystem.Models { public class Department { [Key] public int deptID { get; set; } public string deptName { get; set; } p
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
}
2.1.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>();
}
}
}
数据库:我在数据库中有两个表,1。部门(部门名称、名称、描述)2。名称(设计ID、设计名称、说明)
目标:-我想为这些场景创建一个视图页面。像这样
插入表格名称(文本框)+部门名称(下拉列表框)+指定名称(下拉列表框)
1.c.部门
namespace mvcAppraisalSystem.Models
{
public class Department
{
[Key]
public int deptID { get; set; }
public string deptName { get; set; }
public string Description { get; set; }
}
}
2.1.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>();
}
}
}
namespace mvcAppraisalSystem.Models
{
公共类CompanyDBContext:DbContext
{
public CompanyDBContext():基(“CompanyDBContext”)
{
}
公共数据库集部门{get;set;}
公共数据库集指定{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove();
}
}
}
您正在将集合创建为DbSet
。您需要的是DbSet
和DbSet
公共数据库集部门{get;set;}
公共数据库集指定{get;set;}
这看起来很明显是一个输入错误,但您之所以会出现错误,是因为运行时不知道如何在具有相同元素类型的同一上下文中填充多个对象集。这更像是说:
public DbSet<Department> SomeDepartments { get; set; }
public DbSet<Department> OtherDepartments { get; set; }
公共数据库集{get;set;}
公共数据库集其他部门{get;set;}
因为(大概)你会期望一些东西来定义
SomeDepartments
中的内容,也会有一些东西来定义OtherDepartments
中的内容,而运行时并不知道这一点(并且没有办法表达),这就是为什么你会出现错误。好的,我明白你的意思了。我的目标是,我想为两个模型类创建一个视图页面。就像我说的,我是我的Q?你在问两个不同的问题。你发布的错误是因为我在回答中指出的问题。是的,你完全是赖特。谢谢你们的帮助,但我希望你们能帮助我完成我在问题目标中提到的全部问题。如果你们还有第二个问题,请将其作为单独的问题发布。由于SO旨在成为一个易于搜索的问答网站,因此通常不允许组合问题(因为这样会使查找与某人正在寻找的内容相关的问题变得更加困难)。由于您的问题的标题及其正文大部分都与此错误有关,您可以随意保留此问题(如果它为您解决了该问题,请将此答案标记为已接受),然后针对您的其他问题发布另一个问题。