I am using Entity Framework 6.1.3
and have the two models as shown below. However, when I run migration I get the below error:
Unable to determine the principal end of an association between the types 'Example.Models.GiftVoucher' and 'Example.Models.Payment'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
I have searched for it and found this question. One of the solution was to use the Required
attribute.
However, I don't know how I can do the following in Entity Framework:
- Rename GiftVoucherId and PaymentId in both models that are as foreign keys to
Purchase_Id
andRedemption_Id
as shown in the image. - Then do something equivalent in Entity Framework like this
CONSTRAINT fk-giftvoucher-table FOREIGN KEY (Purchase_Id) REFERENCES PAYMENT (PaymentId)
.
Payment Model
public class Payment {
public int Id { get; set; }
[Required]
public DateTime DateTime { get; set; }
public double Amount { get; set; }
[Required]
public int GiftVoucherId { get; set; }
public GiftVoucher GiftVoucher { get; set; }
}
Gift Voucher Model
public class GiftVoucher
{
public int Id { get; set; }
public double Amount { get; set; }
[Required]
public int PaymentId { get; set; }
public Payment Payment { get; set; }
}