0
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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

The error means that you have data in other tables that references the data you are trying to delete. This is asimilar thread You would need to drop and recreate the constraints.