我对ASP.NET核心脚手架有个问题。我只是在一个简单的关系模型上应用了脚手架,并得到了这个SqlException。
Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'ClassRoomClassId'.'我只有两个模特
ClassRoom
public class ClassRoom
{
[Key]
public int ClassId { get; set; }
public string ClassName { get; set; }
public virtual ICollection<Student> Student { get; set; }
}学生
public class Student
{
[Key]
public int StuId { get; set; }
public string StuName { get; set; }
[ForeignKey("ClassId")]
public int? ClassId { get; set; }
public ClassRoom ClassRoom { get; set; }
}在执行Students/Index.时,我得到了异常
// GET: Students
public IActionResult Index()
{
return View(_context.Student.ToList());
}我没有这个名字'ClassRoomClassId‘的专栏!
会有什么问题?
发布于 2020-06-29 10:11:36
尝试将[ForeignKey("ClassId")]更改为[ForeignKey("ClassRoom")]或将其放在ClassRoom属性上:
public class Student
{
...
public int? ClassId { get; set; }
[ForeignKey("ClassId")]
public ClassRoom ClassRoom { get; set; }
}https://stackoverflow.com/questions/62635731
复制相似问题