gpt4 book ai didi

c# - 试图搭建一个 View ,我得到 "unable to retrieve Metadata for"和 'validation error' 'entity type has no key defined'

转载 作者:太空狗 更新时间:2023-10-29 21:04:49 25 4
gpt4 key购买 nike

我有一个程序,我试图找出我的 friend 没有得到的问题。我正在尝试从我的 Controller 搭建一个 View ,我得到了一个

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 View 模型类构建一个创建 View ,但我总是遇到此错误。

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

我的 View 模型类:

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

}
}

我正在尝试搭建的 Controller 方法

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# - 试图搭建一个 View ,我得到 "unable to retrieve Metadata for"和 'validation error' 'entity type has no key defined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22643817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com