2014-01-06 38 views
6

当我创建控制器的脚手架并添加模型类,然后我得到这些错误“每个类型的多个对象集不支持”。不支持每种类型的多个对象集?

我有三个模型类:

1.Department.CS

2.Designation.cs

3.CompanyDBContext.cs

数据库:我有两个表中的数据库, 1.部门(deptID,deptName,说明)2.指定(desgtID,desgName,说明)

目的: - 我想为这些sce创建一个视图页面nario的。像这样

表格(文本框)+部门名称(下拉列表框)+指定名称(下拉列表框)的

插入名称

1.Department.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>(); 
     } 
    } 
} 

回答

15

您创建的集合为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和运行时并不知道这一点(有没办法表达它),这就是为什么你会收到错误。

+0

好的,我明白了你的观点。我的目标是,我想为这两个模型类创建一个视图页面。就像我说过我的问题? –

+0

你在问两个不同的问题。你发布的错误是因为我在这个答案中指出的问题。 –

+0

是的,你是完全赖特。感谢您的帮助,但我希望得到您在我的问题目标中提到的完整问题的帮助。 –

相关问题