简体   繁体   English

使用外键插入许多值

[英]Insert many value using foreign key

I have two table 我有两个桌子 在此处输入图片说明

questionId is foreign key of Questions's Id. questionId是问题ID的外键。

I'm trying to save 4 answer in "Answers" table. 我正在尝试在“答案”表中保存4个答案。 Title is question's title and it must have 4 answer. 标题是问题的标题,必须有4个答案。

在此处输入图片说明

How can i insert 4 Answer which is connected by foreign key? 如何插入由外键连接的4 Answer?

I have tried this: 我已经试过了:

QuizEntities quiz = new QuizEntities();
Question question = new Question();
Answer answer = new Answer();

question.Title = titleBox.Text;
question.ImageURL = "../Uploads/" + PhotoBox.Text;


 answer.Answer1 = firstAnswer.Text;
 answer.Answer1 = secondAnswer.Text;
 answer.Answer1 = thirdAnswer.Text;
 answer.Answer1 = fourthAnswer.Text;
 answer.questionId = question.Id;
 if (firstCheckBox.Checked)
 {
   answer.IsCorrect = true;
 }
 else
 {
   answer.IsCorrect = false;
 }

 quiz.Questions.Add(question);
 quiz.Answers.Add(answer);
 quiz.SaveChanges();

but using this I can insert only one value. 但是使用这个我只能插入一个值。 How can I solve it? 我该如何解决?

Just create four answers and insert them all, you can create a function to make the code more readable. 只需创建四个答案并将其全部插入即可,您可以创建一个函数以使代码更具可读性。

private static Answer CreateAnswer (
    int id, string answer, bool isCorrect, int questionId)
{    
    var answer = new Answer
    {
        Id = id,
        Answer = answer,
        IsCorrect = isCorrect,
        questionId = questionId
    };
    return answer;
}

Then add a question with for answers by assigning temporary key. 然后通过分配临时密钥添加一个问题以寻求答案。

using (var quiz = new QuizEntities())
{
    var qId = 1;
    var question = new Question
    {
        Id = qId,
        title = titleBox.Text,
        ImageURL = "../Uploads/" + PhotoBox.Text
    };

    quiz.Questions.Add(question);
    quiz.Answers.Add(
        CreateAnswer(1, firstAnswer.Text, (bool)firstCheckBox.Checked, qId));
    quiz.Answers.Add(
        CreateAnswer(2, secondAnswer.Text, (bool)secondCheckBox.Checked, qId));
    quiz.Answers.Add(
        CreateAnswer(3, thirdAnswer.Text, (bool)thirdCheckBox.Checked, qId));
    quiz.Answers.Add(
        CreateAnswer(4, fourthAnswer.Text, (bool)fourthCheckBox.Checked, qId));
    quiz.SaveChanges();
}

Tips: 提示:

  • Wrap the context with using to make sure that it's disposed properly. 使用来包装上下文,以确保其正确放置。
  • Temporary key can be used to create relationship between principal and dependent before the entities are inserted. 可以在插入实体之前使用临时键在主体和从属之间创建关系。

If you want to add exactly four values, you need to create four Answer objects and for each of those assign the correct question ID. 如果要恰好添加四个值,则需要创建四个Answer对象,并为每个对象分配正确的问题ID。

Without testing this with compiler, I think you are looking for something like this; 如果不使用编译器进行测试,我认为您正在寻找类似的东西。

QuizEntities quiz = new QuizEntities();
Question question = new Question();
Answer answer = new Answer();

question.Title = titleBox.Text;
question.ImageURL = "../Uploads/" + PhotoBox.Text;
 quiz.Questions.Add(question);

//Add first answer.
 answer.Answer = firstAnswer.Text;
 if (firstCheckBox.Checked)
 {
   answer.IsCorrect = true;
 }
 else
 {
   answer.IsCorrect = false;
 }

 answer.questionId = question.Id;
 quiz.Answers.Add(answer);

//Add second answer.
answer = new Answer();
 answer.Answer = secondAnswer.Text;
 if (secondCheckBox.Checked)
 {
   answer.IsCorrect = true;
 }
 else
 {
   answer.IsCorrect = false;
 }

 answer.questionId = question.Id;
 quiz.Answers.Add(answer);


//... add third and fourth as well.
 quiz.SaveChanges();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM