当我使用以下代码时,表格会成功生成并包含主键和外键关系。
但是当我使用以下代码时,我遇到了错误:
[Table("tblDepartments")]
public class DepartmentModel
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public ICollection<EmployeeModel> Employees { get; set; }
}
[Table("tblEmployees")]
public class EmployeeModel
{
[Key]
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
public virtual DepartmentModel DID { get; set; }
}
但是当我使用以下代码时,我遇到了错误:
[Table("tblDepartments")]
public class DepartmentModel
{
[Key]
public int DepartmentID { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public ICollection<EmployeeModel> Employees { get; set; }
}
[Table("tblEmployees")]
public class EmployeeModel
{
[Key]
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
[ForeignKey("DeptID")]
public virtual DepartmentModel DID { get; set; }
}
错误:
MvcApplication1.Models.EmployeeModel
类型的属性DID
上的ForeignKeyAttribute
不是有效的。在从属类型MvcApplication1.Models.EmployeeModel
上未找到外键名称DeptID
。Name 值应该是外键属性名称的逗号分隔列表。
请帮忙,谢谢。
DeptID
是EmployeeModel
的属性时,您可以使用此属性。 - Gert Arnold