简体   繁体   English

EF6 CodeFirst ForeignKey属性映射无法按预期工作

[英]EF6 CodeFirst ForeignKey attribute mapping not working as expected

I have a many-to-1 situation where an Employee can serve many Customers and each Customer is served by 1 Employee. 我有一个多对一的情况,一个员工可以为许多客户提供服务,而每个客户由1个员工提供服务。 In the Customer class I map SupportRep to EmployeeId using the ForeignKey attribute and it gives me an error. 在客户类中,我使用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; }
}

The error I get is: 我得到的错误是:

Error: The ForeignKeyAttribute on property 'SupportRep' on type 'SqlLiteChinook. 错误:类型'SqlLiteChinook上属性'SupportRep'上的ForeignKeyAttribute。 Customer' is not valid. 客户'无效。 The foreign key name 'EmployeeId' was not found on the d ependent type 'SqlLiteChinook.Customer'. 在不同类型的“ SqlLiteChinook.Customer”上找不到外键名称“ EmployeeId”。 The Name value should be a comma separa ted list of foreign key property names. Name值应该是外键属性名称的逗号分隔列表。

However, if I change EmployeeId to SupportRepId in the Customer class it works. 但是,如果我在Customer类中将EmployeeId更改为SupportRepId,则它可以工作。

Shouldn't the foreign key in the Customer class point to EmployeeId of Employee class? 客户类中的外键是否不能指向Employee类的EmployeeId?

Please enlighten me. 请赐教。 Thank you. 谢谢。

You're mixing things up a bit. 您正在把事情混在一起。 The [ForeignKey("EmployeeId")] in your Customer class has to say which property of that class is the foreign key to Employee . 您的Customer类中的[ForeignKey("EmployeeId")]必须说出该类的哪个属性是Employee的外键。 The attribute doesn't go looking for the foreign key in the Employee class. 该属性不会在Employee类中查找外键。

Your second mistake is declaring a foreign key on the ICollection<Customer> . 您的第二个错误是在ICollection<Customer>上声明外键。 Since you have a one-to-many relationship, your collection can't point to one customer, it has a relation to multiple ones. 由于您具有一对多关系,因此您的收藏不能指向一个客户,它与多个客户有关系。 So your customer needs to have the foreign key to employee (each customer 'points' to one employee) but employee can't contain a foreign key to customer. 因此,您的客户需要拥有员工的外键(每个客户“指向”一名员工),但是员工不能包含客户的外键。

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; }
}

Try this 尝试这个

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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM