Trying to scaffold a view and I get "unable to retrieve Metadata for" and 'validation error' 'entity type has no key defined'

I have a program where I am trying to figure out an issue that my friends are not getting. I am trying to scaffold a view from my controller, and I get an

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 scaffold a create view using the CourseAddForm view model class but I always get this error.

My CourseAddForm class is not supposed to carry a key field, my friends do not have this problem that I am having.

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 that I am trying to scaffold

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" />

Answers


All I had to do was remove the datacontext option in my scaffolding and it works...


I'm wondering if you're inheriting in the wrong direction. You have a primary key on CourseBase, but CourseBase is inherting CourseAdd. So all the properties of CourseAdd are apart of CourseBase, but not the other way. So there's no primary key on CourseAdd, as the cryptic error message is trying to say with "no keys defined".

Try:

public class CourseAdd : CourseBase {
    // trim
}

and make CourseBase not inherit CourseAdd.


Need Your Help

Error on creating new Git project in Visual Studio 2013

c# git version-control visual-studio-2013

I'm using Visual Studio 2013 with the MS Git plugin. I'm trying to add an existing project to source control on my machine. The project is an empty project with one file. The path to the project so...