C
C#8mo ago
Iago

SqlException: The INSERT statement conflicted with the FOREIGN KEY

Guys, I have this error in ASP.NET 8 "Internal Exception 1: SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_RegistrationFormModels_SportModel_SportId". The conflict occurred in database "RegistrationFormDb", table "dbo.SportModel", column 'Id'." I am inserting the value 2 in this sport_id, but this value 2 exists in the bank. I don't know why it's giving this error
4 Replies
Giovani Ferro
Giovani Ferro8mo ago
are you using C# with dapper or EF? the fk is correctly bonded with pk_sport_Id?
Iago
IagoOP8mo ago
I'm using EF. I'm not sure, my model is like this
public class RegistrationFormModel
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateTime { get; set; }
public string GeneralInformations { get; set; }
public int GenderId { get; set; }
public string BirthCertificateImageUrl { get; set; }

//Endereços
public string CEP { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public string City { get; set; }
public string State { get; set; }

// Responsible Informations
public string ResponsibleName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }

//Foreign Keys
public int StudyShift_id { get; set; }
public int Sport_id { get; set; }

//Navigation Properties
public GenderModel Gender { get; set; }
public StudyShiftModel StudyShift { get; set; }
public SportModel Sport { get; set; }

}
public class RegistrationFormModel
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateTime { get; set; }
public string GeneralInformations { get; set; }
public int GenderId { get; set; }
public string BirthCertificateImageUrl { get; set; }

//Endereços
public string CEP { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public string City { get; set; }
public string State { get; set; }

// Responsible Informations
public string ResponsibleName { get; set; }
public string PhoneNumber { get; set; }
public string Email { get; set; }

//Foreign Keys
public int StudyShift_id { get; set; }
public int Sport_id { get; set; }

//Navigation Properties
public GenderModel Gender { get; set; }
public StudyShiftModel StudyShift { get; set; }
public SportModel Sport { get; set; }

}
Angius
Angius8mo ago
show code
Pobiega
Pobiega8mo ago
sport_id doesn't follow convention for what a foreign key should be named in EF, so this won't work unless you did some entity configuration that you have not shared
Want results from more Discord servers?
Add your server