私は外部キー関係に私のクラスで間違った方法を定義していたので、私はこれと同じエラーが発生しました:
public partial class Student
{
[Key]
public int StudentId { get; set; }
public string StudentName { get; set; }
public int StudentAge { get; set; }
public int GradeNumber { get; set; }
public string LockerNumber { get; set; }
public string TeacherID { get; set; }
public string StudentComments { get; set; }
// Navigation properties
[ForeignKey("TeacherID")]
public virtual Teacher Teacher { get; set; }
public virtual GradeLevel GradeLevel { get; set; }
// the code below is incorrect, the StudentClass should have the StudentId
// as the FK entity:
[ForeignKey("StudentId")]
public virtual StudentClass StudentClass { get; set; }
}
StudentClassクラス
public partial class StudentClass
{
[Key]
public int RowId { get; set; }
public int StudentId { get; set; }
public int ClassSubjectId { get; set; }
// Navigation properties
[ForeignKey("ClassSubjectId")]
public virtual ClassSubject ClassSubject { get; set; }
// this is the way the relationship should be defined:
[ForeignKey("StudentId")]
public virtual Student Student { get; set; }
}
は、SQL Server側の自動インクリメントのID列です? –
はい。問題ありますか ? –
あなたは正しいですか。あなたのID列がオートインクリメントintステップ1であればOKです。しかし、以下のように、その列に明示的な値を追加するべきではありません。 –