EF6 CodeFirst ForeignKey 属性映射未按预期工作
EF6 CodeFirst ForeignKey attribute mapping not working as expected
我有一个多对一的情况,一个员工可以服务很多客户,每个客户由 1 名员工服务。在 Customer class 中,我使用 ForeignKey 属性将 SupportRep 映射到 EmployeeId,它给我一个错误。
public class Customer
{
[Key]
public int CustomerId { get; set; }
...
public int? SupportRepId { get; set; }
[ForeignKey("EmployeeId")]
public virtual Employee SupportRep { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
...
[ForeignKey("SupportRepId")]
public virtual ICollection<Customer> Customers { get; set; }
}
我得到的错误是:
Error: The ForeignKeyAttribute on property 'SupportRep' on type 'SqlLiteChinook.
Customer' is not valid. The foreign key name 'EmployeeId' was not found on the d
ependent type 'SqlLiteChinook.Customer'. The Name value should be a comma separa
ted list of foreign key property names.
但是,如果我将 Customer class 中的 EmployeeId 更改为 SupportRepId,它会起作用。
Customerclass中的外键不应该指向Employeeclass的EmployeeId吗?
请赐教。谢谢。
试试这个
public class Customer
{
[Key]
public int CustomerId { get; set; }
...
public int? SupportRepId { get; set; }
[ForeignKey("SupportRepId")]
public virtual Employee SupportRep { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
...
public virtual ICollection<Customer> Customers { get; set; }
}
你把事情搞混了。 Customer
class 中的 [ForeignKey("EmployeeId")]
必须说明 class 中的哪个 属性 是 Employee
的外键。该属性不会在 Employee
class.
中寻找外键
您的第二个错误是在 ICollection<Customer>
上声明外键。由于您有一对多关系,您的集合不能指向 一个 客户,它与多个客户有关系。因此,您的客户需要拥有员工的外键(每个客户 'points' 到一名员工),但员工不能包含客户的外键。
public class Customer
{
[Key]
public int CustomerId { get; set; }
...
public int? SupportRepId { get; set; }
[ForeignKey("SupportRepId")]
public virtual Employee SupportRep { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
...
public virtual ICollection<Customer> Customers { get; set; }
}
我有一个多对一的情况,一个员工可以服务很多客户,每个客户由 1 名员工服务。在 Customer class 中,我使用 ForeignKey 属性将 SupportRep 映射到 EmployeeId,它给我一个错误。
public class Customer
{
[Key]
public int CustomerId { get; set; }
...
public int? SupportRepId { get; set; }
[ForeignKey("EmployeeId")]
public virtual Employee SupportRep { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
...
[ForeignKey("SupportRepId")]
public virtual ICollection<Customer> Customers { get; set; }
}
我得到的错误是:
Error: The ForeignKeyAttribute on property 'SupportRep' on type 'SqlLiteChinook. Customer' is not valid. The foreign key name 'EmployeeId' was not found on the d ependent type 'SqlLiteChinook.Customer'. The Name value should be a comma separa ted list of foreign key property names.
但是,如果我将 Customer class 中的 EmployeeId 更改为 SupportRepId,它会起作用。
Customerclass中的外键不应该指向Employeeclass的EmployeeId吗?
请赐教。谢谢。
试试这个
public class Customer
{
[Key]
public int CustomerId { get; set; }
...
public int? SupportRepId { get; set; }
[ForeignKey("SupportRepId")]
public virtual Employee SupportRep { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
...
public virtual ICollection<Customer> Customers { get; set; }
}
你把事情搞混了。 Customer
class 中的 [ForeignKey("EmployeeId")]
必须说明 class 中的哪个 属性 是 Employee
的外键。该属性不会在 Employee
class.
您的第二个错误是在 ICollection<Customer>
上声明外键。由于您有一对多关系,您的集合不能指向 一个 客户,它与多个客户有关系。因此,您的客户需要拥有员工的外键(每个客户 'points' 到一名员工),但员工不能包含客户的外键。
public class Customer
{
[Key]
public int CustomerId { get; set; }
...
public int? SupportRepId { get; set; }
[ForeignKey("SupportRepId")]
public virtual Employee SupportRep { get; set; }
}
public class Employee
{
[Key]
public int EmployeeId { get; set; }
...
public virtual ICollection<Customer> Customers { get; set; }
}