0

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

}

enter image description here

Mr Nobody
  • 359
  • 3
  • 9
  • 23

1 Answers1

2

I don't tend to use one-to-one relationships a lot... Unless i want to Vertically Partition for performance needs or there is an incessant OCD need to separate the concerns.

*Note: One-to-one relationship is technically not possible in SQL Server. It will always be one-to-zero-or-one. EF forms One-to-One relationships on entities not in DB.*

However, documentation is your friend

Configuring a Relationship Where Both Ends Are Required (One-to-One)

In most cases the Entity Framework can infer which type is the dependent and which is the principal in a relationship. However, when both ends of the relationship are required or both sides are optional the Entity Framework cannot identify the dependent and principal. When both ends of the relationship are required, use WithRequiredPrincipal or WithRequiredDependent after the HasRequired method. ...

Given the following

public class GiftVoucher
{
    // your primary key
    public int GiftVoucherId { get; set; }
    public virtual Payment Payment { get; set; }

    // other properties
    public double Amount { get; set; }
}

public class Payment 
{ 
    // We need to share the same key
    public int GiftVoucherId { get; set; }
    public virtual GiftVoucher GiftVoucher { get; set; }

    // other properties
    public DateTime DateTime { get; set; }
    public double Amount { get; set; }
}

We can Fluent API like this

// Configure the primary key for the OfficeAssignment 
modelBuilder.Entity<Payment>() 
    .HasKey(t => t.GiftVoucherId); 

// we are essentially making GiftVoucher the principle in the DB
modelBuilder.Entity<GiftVoucher>() 
    .HasRequired(t => t.Payment) 
    .WithRequiredPrincipal(t => t.GiftVoucher);

So basically HasRequired is making GiftVoucher required and WithRequiredPrincipal is making Payment required.. In turn, EF will throw if the above is not satisfied

TheGeneral
  • 79,002
  • 9
  • 103
  • 141