I am trying to save data into the Doctor
table in the database. The table structure is shown below.
The error I get when saving
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
Microsoft.Data.SqlClient.SqlException (0x80131904): The MERGE statement conflicted with the FOREIGN KEY constraint "FK_DoctorClinic_Clinic_DoctorId"
Table structure:
Doctor
Id
Name
Clinic
Id
ClinicName
DoctorClinic
DoctorId
ClinicId
Code:
foreach (var clinicId in clinicIds)
{
DoctorClinic doctorClinic = new DoctorClinic()
{
ClinicId = clinicId,
DoctorId = doctor.Id
};
doctorClinicList.Add(doctorClinic);
}
doctor.doctorClinic = doctorClinicList;
var doc = await _docRepo.Create(doctor);
Note: I think the reason is that DoctorClinic
requires a doctorID
, and we don't have the ID as we have not saved it yet.
User contributions licensed under CC BY-SA 3.0