c# – 尝试构建一个视图并且“无法检索元数据”和“验证错误”实体类型没有定义键

前端之家收集整理的这篇文章主要介绍了c# – 尝试构建一个视图并且“无法检索元数据”和“验证错误”实体类型没有定义键前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个程序,我试图弄清楚我的朋友没有得到的问题.我试图从我的控制器构建一个视图,然后我得到一个
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.'

我试图使用CourseAddForm视图模型类来构建一个创建视图,但我总是得到这个错误.

我的CourseAddForm类不应该带有关键字段,我的朋友没有我遇到的这个问题.

我的视图模型类:

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; }

}
}

我试图搭建的控制器方法

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);
        }
    }

我的连接字符串:

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

解决方法

我所要做的就是删除我的脚手架中的datacontext选项,它的工作原理……

猜你在找的C#相关文章