I am working on a project and there is a trainer I follow.When I came to the update process, I started to get an error, when I went with Breakpoints, I realized that the QuizId value came 0000 in the save phase. When I check the database, all my connections are correct, id values are also available, but I have a problem with the output.
Questions.cs
[Key]public Guid Id { get; set; }public string Text { get; set; }public Guid QuizId { get; set; }[ForeignKey(nameof(QuizId))]public virtual Quiz Quiz { get; set; }public virtual ICollection<Options> Options { get; set; } = [];Options.cs
[Key]public int Id { get; set; }public string Text { get; set; }public bool IsCorrect { get; set; }public Guid QuestionId { get; set; }[ForeignKey(nameof(QuestionId))]public virtual Questions Questions { get; set; }Quiz.cs
[Key] public Guid Id { get; set; } public string? Name { get; set; } public int TotalQuestions { get; set; } public int TimeInMinutes { get; set; } public bool IsActive { get; set; } public Guid CategoryId { get; set; } [ForeignKey(nameof(CategoryId))] public virtual Category? Category { get; set; } public ICollection<Questions> Questions { get; set; } = [];below is the block of code I used to save the data
public async Task<QuizApiResponse> SaveQuizAsync(QuizSaveDto dto){ var questions = dto.Question.Select(q => new Questions { Id = Guid.NewGuid(), Text = q.Text, Options = q.Option.Select(o => new Options { Id = 0, Text = o.Text, IsCorrect = o.IsCorrect }).ToArray() }).ToArray(); if (dto.Id == Guid.Empty) { var quiz = new Quiz { Id = Guid.NewGuid(), Name = dto.Name, CategoryId = dto.CategoryId, TotalQuestions = dto.TotalQuestions, TimeInMinutes = dto.TimeInMinutes, IsActive = dto.IsActive, Questions = questions }; _context.Quizzes.Add(quiz); } else { var dbQuiz = await _context.Quizzes.FirstOrDefaultAsync(q => q.Id == dto.Id);//QuizId have if (dbQuiz == null) { return QuizApiResponse.Fail("Quiz doesn't exists"); } dbQuiz.CategoryId = dto.CategoryId; dbQuiz.IsActive = dto.IsActive; dbQuiz.Name = dto.Name; dbQuiz.TimeInMinutes = dto.TimeInMinutes; dbQuiz.TotalQuestions = dto.TotalQuestions; dbQuiz.Questions = questions;//QuizId is 0000 _context.Quizzes.Update(dbQuiz);// burada QuizId null } try { await _context.SaveChangesAsync(); return QuizApiResponse.Success(); } catch (Exception ex) { return QuizApiResponse.Fail(ex.Message); }}here is a visual of my database schema
the point I detected when I proceeded with breakpoint
the error I get is as follows
Microsoft.EntityFrameworkCore.Update[10000]An exception occurred in the database while saving changes for context type 'BlazingQuiz.Api.Data.Repositories.QuizContext'.Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.---> Microsoft.Data.SqlClient.SqlException (0x80131904): The MERGE statement conflicted with the FOREIGN KEY constraint "FK_Options_Questions_QuestionId". The conflict occurred in database "BlazingQuiz", table "dbo.Questions", column 'Id'.at Microsoft.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action
1 wrapCloseInAction) at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)at Microsoft.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)