我有同樣的錯誤,因爲我已經在我的類中定義一個外鍵關係的錯誤的方式:
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端自動增量標識列? – 2012-04-08 07:09:19
是的。那是問題嗎 ? – 2012-04-08 07:26:52
不,你是對的。如果你的Id列是Autoincrement int step 1,你可以。但正如下面所說的那樣,您不應該爲該列附加明確的值。 – 2012-04-08 07:35:14