我无法将新对象添加到数据库中

时间:2016-12-05 11:09:13

标签: asp.net asp.net-mvc entity-framework

我有两个模型:舞台和测验

public class Stage
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid StageId { get; set; }

    (...)

    public virtual Quiz Quiz { get; set; }
}

public class Quiz
{
    [Key, ForeignKey("Stage")]
    public Guid StageId { get; set; }

    public virtual List<Question> Questions { get; set; }

    public virtual List<ApplicationUser> Users { get; set; }

    public virtual Stage Stage { get; set; }

    public Quiz()
    {

        Questions = new List<Question>();
        Users = new List<ApplicationUser>();
    }
}

和创建方法:

public Guid Create(NewQuizViewModel quizToCreate)
    {
        Stage currentStage = _stageService.GetStageById(quizToCreate.StageId);

        List<Question> newQuestions = new List<Question>();

        foreach (var question in quizToCreate.Questions)
        {
            Question newQuestion = new Question();
            newQuestion.QuestionId = Guid.NewGuid();
            newQuestion.QuestionContent = question.QuestionContent;
            newQuestion.Answers = question.Answers;
            newQuestion.CorrectAnswer = question.CorrectAnswer;
            newQuestions.Add(newQuestion);
        }

        Quiz quizToDatabase = new Quiz()
        {
            Questions = newQuestions,
            Stage = currentStage,
        };

        _dbContext.Quizzes.Add(quizToDatabase);
        _dbContext.SaveChanges();

        return quizToDatabase.StageId;
    }

你能告诉我代码有什么问题吗?我无法将Quiz添加到数据库,StageId为空。我有错误

_dbContext.Quizzes.Add(quizToDatabase);

截图: Adding to database

在Seed方法中一切正常。

var quizzes = new List<Quiz>()
        {
            new Quiz()
            {
                Questions = new List<Question>()
                {
                    new Question()
                    {
                        QuestionContent = "Just mark 1st",
                        Answers = new List<string>(4)
                        {
                            "First", "Second", "Third", "Fourth"
                        },
                        CorrectAnswer = CorrectAnswer.FirstAnswer
                    },
                    new Question()
                    {
                        QuestionContent = "Just mark 3rd",
                        Answers = new List<string>(4)
                        {
                            "First", "Second", "Third", "Fourth"
                        },
                        CorrectAnswer = CorrectAnswer.ThirdAnswer
                    },
                    new Question()
                    {
                        QuestionContent = "Just mark 4th",
                        Answers = new List<string>(4)
                        {
                            "First", "Second", "Third", "Fourth"
                        },
                        CorrectAnswer = CorrectAnswer.FourthAnswer
                    }
                },
                Stage = context.Stages.FirstOrDefault(c => c.Name=="Stage Example")

            }
        };

        quizzes.ForEach(c => context.Quizzes.Add(c));
        context.SaveChanges();

我看不出差异。谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

我认为这是因为你从“_stageService”(第一个dbcontext!)和“_dbContext”(第二个dbcontext)上的SaveChanges获得“currentStage”。 你不能混用dbContext ...