c# - 不支持每种类型的多个对象集?

标签 c# asp.net visual-studio-2013 asp.net-mvc-5

当我为 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/

相关文章:

c# - 比较 float 的总和

c# - IIS7 文件映射 - .asax、.ashx、.asap

c# - 来自 C# 的自定义 ajax 错误

c++ - Visual Studio 2013 Professional 链接器错误

c# - 正则表达式模式数字后跟一个字符

c# - MVC 中的服务总线 + webjob。将字符串作为消息发送

c# - Linux Ubuntu 单声道

javascript - 如何使用导入语句传递凭据?

c# - 调试 C# 中的 VS 与 SQL Server 之间的连接

visual-studio-2013 - IISexpress重置并设置配置目录