试图构建一个视图,我得到了“;无法检索“”的元数据;和';验证错误'';实体类型没有定义键';
本文关键字:元数据 验证 定义 类型 错误 实体 检索 构建 一个 视图 | 更新日期: 2023-09-27 17:53:43
我有一个程序,我试图找出我的朋友们没有得到的问题。我正试图从我的控制器中构建一个视图,我得到了一个
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选项,它就可以工作了。。。
我想知道您的继承方向是否错误。您在CourseBase
上有一个主键,但CourseBase
在承载CourseAdd
。因此,CourseAdd
的所有性质都是CourseBase
的一部分,而不是相反。因此,CourseAdd
上没有主键,正如神秘的错误消息试图说的那样,"没有定义主键"。
尝试:
public class CourseAdd : CourseBase {
// trim
}
并使CCD_ 7不继承CCD_。