2016-12-05 26 views
0

我有兩個型號:舞臺和測驗我不能添加新的對象數據庫

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; 
    } 

你能告訴我什麼是錯誤的代碼?我無法將測驗添加到數據庫,StageId爲空。我有錯誤的行

_dbContext.Quizzes.Add(quizToDatabase); 

截圖: Adding to database

種子法一切正常。

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(); 

我看不出差別。感謝您的幫助

+0

是否'舞臺currentStage = _stageService.GetStageById(quizToCreate.StageId);'找一個階段? – user1429080

+0

是的,即使在圖片對象quizToDatabase有階段但不是StageId – Dre

+2

什麼是錯誤?在* question *中發佈完整的異常(包括調用堆棧)。你可以用'Exception.ToString()' –

回答

0

我想,那是因爲你從 「_stageService」(第一的DbContext!)得到 「currentStage」 和 「_dbContext」(第二的DbContext)的SaveChanges。 不能混合的DbContext ...

相關問題