简体   繁体   English

实体框架:外键

[英]Entity Framework: Foreign Key

I have 2 model classes: 我有2个模型类:

public class Nurse
{
    public int Id { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string Surname { get; set; }

    [Required]
    public string AddressLine1 { get; set; }
    [Required]
    public string AddressLine2 { get; set; }
    public string AddressLine3 { get; set; }
    public string AddressLine4 { get; set; }

    [Required]
    [EmailAddress]
    public string EmailAddress { get; set; }

    [Required]
    [Phone]
    public string ContactNumber { get; set; }

    [Required]
    public DateTime DateOfBirth { get; set; }

    [Required]
    public DateTime RegistrationDate { get; set; }


    //Foreign Keys

    public int PaymentId { get; set; }
    [ForeignKey("PaymentId")]
    public virtual Payment Payment { get; set; }


    public int BranchId { get; set; }
    [Required]
    [ForeignKey("BranchId")]
    public virtual Branch Branch { get; set; }
}  // Cls

and

public class Payment
{
    public int Id { get; set; }

    //Type of String instead of to allow for starting zeros
    [RegularExpression("^[0-9]+$")]
    public string SortCode { get; set; }

    [RegularExpression("^[0-9]+$")]
    public string BankAccountNumber { get; set; }

    [RegularExpression("^[0-9]+$")]
    public string ChequeNumber { get; set; }

    public DateTime DateReceived { get; set; }

    public string Notes { get; set; }

    //Foreign Keys
    public int PaymentTypeId { get; set; }
    [Required]
    [ForeignKey("PaymentTypeId")]
    public PaymentType PaymentType { get; set; }

    public int NurseId { get; set; }
    [Required]
    [ForeignKey("NurseId")]
    public Nurse Nurse { get; set; }
}  // Cls

Whenever I try to run the application I get the following error: 每当我尝试运行该应用程序时,都会出现以下错误:

Exception thrown: 'System.InvalidOperationException' in EntityFramework.dll 引发异常:EntityFramework.dll中的“ System.InvalidOperationException”

Additional information: The ForeignKeyAttribute on property 'Payment' on type 'PNA_Model.Nurse' is not valid. 附加信息:类型为“ PNA_Model.Nurse”的属性“ Payment”上的ForeignKeyAttribute无效。 The foreign key name 'PaymentId' was not found on the dependent type 'PNA_Model.Payment'. 在从属类型“ PNA_Model.Payment”上找不到外键名称“ PaymentId”。 The Name value should be a comma separated list of foreign key property names. Name值应该是逗号分隔的外键属性名称列表。

Each payment belongs to a nurse but not every nurse has a payment. 每笔付款都属于一名护士,但并非每位护士都有一笔付款。

If I change the 'Id' property of Payment to 'PaymentId' the error goes away, which kind of makes sense according to the error message. 如果我将Payment的'Id'属性更改为'PaymentId',该错误就会消失,根据错误消息,这是有意义的。

However I thought EF was smart enough to figure this out itself and I have similar situations with other classes and their 'Id' properties and I'm not getting error messages for them. 但是我认为EF足够聪明,可以自己解决这个问题,并且我在其他类及其“ Id”属性中也遇到类似的情况,并且没有收到有关它们的错误消息。

Any ideas? 有任何想法吗?

Thanks 谢谢

OK, I think I found what I was doing wrong. 好的,我想我发现自己做错了。

First of all, Entity Framework requires that in 1-to-1 relationships the Primary Key of the dependent (Payment) must also be the Foreign Key. 首先,实体框架要求在一对一关系中,从属(付款)的主键也必须是外键。

So in class 'Nurse' i don't need the 'PaymentId' property because in the Payment class the Key (Payment.Id) will also be the Foreign Key for Nurse and will therefore be equal to the Nurse Key (Nurse.Id). 因此,在“护士”类中,我不需要“ PaymentId”属性,因为在“付款”类中,密钥(Payment.Id)也将是Nurse的外键,因此将与Nurse Key(Nurse.Id)相等。 。 In the Payments class I should remove the 'NurseId' property because this is going to be dealt with in the Key/ForeignKey 在Payments类中,我应该删除“ NurseId”属性,因为这将在Key / ForeignKey中处理

I can do the rest in 2 ways. 我可以通过2种方式来完成其余的工作。

  1. In Payment Class - Use data annotation on the Key (Payment.Id) 在付款类别中-在密钥(Payment.Id)上使用数据注释

     [ForeignKey("Nurse")] public int Id { get; set; } 
  2. In PaymentConfig Class - 在PaymentConfig类中-

     HasRequired(p => p.Nurse) .WithOptional(n => n.Payment); 

The 2nd option might be better if you also want to specify 如果您还想指定第二个选项可能会更好

WillCascadeOnDelete(false)

Thanks to all who helped! 感谢所有的帮助!

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

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