Assp.net 核心异常('​​无效的列名'ClassRoomClassId')

Assp.net Core exeption ('Invalid column name 'ClassRoomClassId')

我对 ASP.NET 核心脚手架有疑问。我刚刚在一个简单的关系模型上应用了脚手架,我得到了这个 SqlException。

Microsoft.Data.SqlClient.SqlException: 'Invalid column name 'ClassRoomClassId'.'

全部我有两个型号

教室

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' !!
会有什么问题?

尝试将 [ForeignKey("ClassId")] 更改为 [ForeignKey("ClassRoom")] 或将其置于 ClassRoom 属性:

public class Student
{
    ...
    public int? ClassId { get; set; }
    [ForeignKey("ClassId")]
    public ClassRoom ClassRoom { get; set; }
}