Ich habe drei Tabellen, die durch Fremdschlüssel verbunden sind. Ich versuche, 1 Zeile in der Fragetabelle und zwei Zeilen in den anderen zwei Tabellen einzufügen. Ich erhalte die Fehlermeldung ‚Anweisung Einfügen Konflikt mit Fremdschlüssel‘ Vielen Dank im Voraus für die HilfeMehrere zusammenhängende Tabellen in Entity auf einmal einfügen
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);
}
Können Sie den Modellcode anzeigen? – Sampath
Vielen Dank Ich habe den Teil des Modells hinzugefügt. – user6934713