public class HeadDoctor
{
public int Id { get; set; }
public int DoctorId { get; set; }
public Doctor Doctor { get; set; }
public int? InstitutionId { get; set; }
public Institution? Institution { get; set; }
}
public class Institution
{
public int Id { get; set; }
public string? Name { get; set; }
public HeadDoctor HeadDoctor { get; set; }
}
In DbContext:
builder.Entity<Institution>()
.HasOne(institution => institution.HeadDoctor)
.WithOne(headDoctor => headDoctor.Institution)
.HasForeignKey<HeadDoctor>(headDoctor => headDoctor.InstitutionId);
Error:
SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_HeadDoctor_Institution_InstitutionId". The conflict occurred in database "MedicDb", table "dbo.HeadDoctor", column 'InstitutionId'.
How to fix this error?