Are multiple sets of objects for each type not supported?

When I create a Scaffold for the Controller and add the Model class, I get this error " Many sets of objects for each type are not supported ."

I have three Model classes:

1.Department.CS

2.Designation.cs

3.CompanyDBContext.cs

Database: I have two tables in the database, 1. Department (deptID, deptName, Description) 2. Designation (desgtID, desgName, description)

Purpose: - I want to create one view page for this scenario. Like this

Insert form name (TextBox) + Department name (drop-down box) + Name name (drop-down list)

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>(); } } } 
+6
source share
1 answer

You create your settings as DbSet<CompanyDBContext> . You want DbSet<Department> and DbSet<Designation> .

  public DbSet<Department> Departments { get; set; } public DbSet<Designation> Designations { get; set; } 

Obviously this is a typo, but the reason you get the error message is because the runtime does not know how to populate many sets of objects in the same context that has the same element type. This would be more like saying:

 public DbSet<Department> SomeDepartments { get; set; } public DbSet<Department> OtherDepartments { get; set; } 

Since (presumably) you expected something to determine what would happen in SomeDepartments , and it would be something to determine that in OtherDepartments , and the runtime does not know this (and there is no way to express it), why you get an error.

+15
source

All Articles