Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 678
user2981393 Avatar asked Mar 25 '14 18:03

user2981393


1 Answers

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

like image 54
user2981393 Avatar answered Nov 20 '22 08:11

user2981393