Trying to look up, and I get "inability to get metadata for" and "validation error", the entity type does not have a key defined '

I have a program in which I try to find out what my friends are not getting. I try to raise the view from my controller and I get

error "Unable to retrieve metadata for 'GradedWork.Controllers.CourseAddForm'. One or more validation errors were detected during model generation: GradedWork.Model.CourseAddForm : EntityType 'CourseAddForm' has no key defined. Define the key for this EntityType. CourseAddForm: EntityType: EntitySet 'CourseAddForm' is based on type 'CourseAddForm' that has no keys defined.' 

I am trying to create a view view using the CourseAddForm view model class, but always get this error.

There should not be a key field in my CourseAddForm class, my friends do not have this problem that I have.

My view model class:

 namespace GradedWork.Controllers { public class CourseList { [Key] public int Id { get; set; } [Display(Name = "Name")] public string Name { get; set; } } public class CourseAddForm { [Display(Name = "Course Code")] public int CourseCode { get; set; } // [Required] [Display(Name = "Name")] public string Name { get; set; } // [Required] [Display(Name = "Semester")] public string Semester { get; set; } // [Required] [Display(Name = "Section Id")] public string SectionId { get; set; } [Display(Name = "Teacher")] public ICollection<TeacherList> Teacher { get; set; } [Display(Name = "Student")] public ICollection<StudentList> Students { get; set; } [Display(Name = "Graded Works")] public ICollection<GradedWorkList> GradedWorks { get; set; } public CourseAddForm() { this.Teacher = new List<TeacherList>(); this.Students = new List<StudentList>(); this.GradedWorks = new List<GradedWorkList>(); } } public class CourseAdd { [Display(Name = "Course Code")] public int CourseCode { get; set; } // [Required] [Display(Name = "Name")] public string Name { get; set; } // [Required] [Display(Name = "Semester")] public string Semester { get; set; } // [Required] [Display(Name = "Section Id")] public string SectionId { get; set; } [Display(Name = "Teacher")] public ICollection<Teacher> Teacher { get; set; } [Display(Name = "Student")] public ICollection<Student> Students { get; set; } [Display(Name = "Graded Works")] public ICollection<GradedWorks> GradedWorks { get; set; } public CourseAdd() { this.Teacher = new List<Teacher>(); this.Students = new List<Student>(); this.GradedWorks = new List<GradedWorks>(); } } public class CourseBase : CourseAdd { [Key] public int Id { get; set; } } } 

My controller method I'm trying to use

 public ActionResult Create() { var addForm = new CourseAddForm(); foreach (var item in m.GetAllTeachersAsList()) { addForm.Teacher.Add(item); } foreach (var item in m.GetAllStudentsAsList()) { addForm.Students.Add(item); } foreach (var item in m.GetAllGradedWorkAsList()) { addForm.GradedWorks.Add(item); } return View(addForm); } // // POST: /Course/Create [HttpPost] public ActionResult Create(CourseAdd newItem) { if (ModelState.IsValid) { var addedItem = m.AddCourse(newItem); if (addedItem == null) { return RedirectToAction("Index"); } else { return RedirectToAction("Details", new { id = addedItem.Id }); } } else { var addForm = Mapper.Map<CourseAddForm>(newItem); foreach (var item in m.GetAllTeachersAsList()) { addForm.Teacher.Add(item); } foreach (var item in m.GetAllStudentsAsList()) { addForm.Students.Add(item); } foreach (var item in m.GetAllGradedWorkAsList()) { addForm.GradedWorks.Add(item); } return View(addForm); } } 

My connection string:

 <connectionStrings> <add name="DataContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\StoreName.mdf;Initial Catalog=StoreName;Integrated Security=True" providerName="System.Data.SqlClient" /> 

+6
source share
2 answers

All I had to do was remove the datacontext parameter in my subclass and it works ...

+12
source

I am wondering if you are inheriting the wrong direction. You have a primary key on CourseBase , but CourseBase is an inheritance of CourseAdd . Thus, all CourseAdd properties CourseAdd different from CourseBase , but not vice versa. Thus, CourseAdd does not have a primary key, because a critical error message tries to say "without keys."

Try:

 public class CourseAdd : CourseBase { // trim } 

and make CourseBase not inherit from CourseAdd .

0
source

All Articles