Just a little concern on how to configure EF for a one to one relationship with same Entity which is a rare case for me. This requirement came to me and i was a bit skeptical on how to model this.
Here is the scenario,
A patient can have one partner and a partner can only belong to one patient. A partner is also a patient.
I have my model like this;
public class Patient
{
public long Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public EGender Gender { get; set; }
public DateTime DateOfBirth { get; set; }
public DateTime CreatedAt { get; set; }
public long PartnerId { get; set; }
public Patient Partner { get; set; }
public EPatientType PatientType { get; set; }
}
And my FluentAPI config like this, but not sure i got this right
builder.HasOne(p => p.Partner)
.WithOne(p => p.Partner) // Not sure what to do here
.IsRequired(false)
.HasForeignKey<Patient>(p => p.PartnerId)
.OnDelete(DeleteBehavior.Restrict);
Any idea on how to model this on fluentAPI or how to proceed is appreciated...
[索引(“IX_UniqueParentId”,1,IsUnique = true)]
public long? PartnerId { get; set; }
Try to use:
builder.HasOne(p => p.Partner)
.WithOne()
.IsRequired(false)
.HasForeignKey<Patient>(p => p.PartnerId);
It worked for me.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.