6

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

user2981393
  • 519
  • 1
  • 8
  • 18

2 Answers2

12

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

user2981393
  • 519
  • 1
  • 8
  • 18
0

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.

Steven V
  • 16,357
  • 3
  • 63
  • 76
  • That is not really an option because the way my classes are set up now are the conventional way that my teacher wants it to be like, so base is supposed to inherit add, and add is a plus of base, for some reason – user2981393 Mar 25 '14 at 19:20
  • A base class implies that it is abstract and the class should be inherited _from_. Plus each entity/database tabble [should have a primary key](http://stackoverflow.com/questions/840162/should-each-and-every-table-have-a-primary-key). – Steven V Mar 25 '14 at 19:24
  • That seems to be the general consensus on stackoverflow but for my case addform and add cannot have a id and a [key]. My friends do not have this issue for some reason. – user2981393 Mar 25 '14 at 19:30