I have three tables that are connected by foreign keys. I am trying to insert 1 row in the question table and two rows in the other two tables. I am getting the error 'Insert statement conflict with Foreign Key constraint' Thank you in advance for the help
public void setMultiAnswer()
{
try
{
string question = "Question 1"
responsesList.Add("Answer1");
responsesList.Add("Answer2");
questionResponsesList.Add(false);
questionResponsesList.Add(true);
using (Entities testEntity = new Entities())
{
Question questionObj = new Question();
questionObj.Question1 = question;
questionObj.CreatedBy = "test";
questionObj.CreatedDate = DateTime.Now;
QuestionRespons questionResponsesObj = new QuestionRespons();
// fill response
foreach (var questionResponse in questionResponsesList)
{
questionResponsesObj.CorrectResponse = questionResponse;
}
questionObj.QuestionResponses.Add(questionResponsesObj);
Response responseObj = new Response();
// fill response
foreach (var response in responsesList)
{
responseObj.Response1 = response;
responseObj.CreatedBy = "test";
responseObj.CreatedDate = DateTime.Now;
}
questionResponsesObj.Response = responseObj;
testEntity.Questions.Add(questionObj);
testEntity.SaveChanges();
}
}
catch (Exception ex)
{
Console.Write(ex);
}