The property 'XID' on entity type 'X' is part of a key and so cannot be modified

678 views Asked by At

Using EF-Core Code First in a new Blazor project. This is the first time I've used the core version of EF so maybe it's something new I missed?

I have three tables/models - Questions, QuestionOptions, and QuestionAnswers. The QuestionOptions are related to both the Questions and Answers tables, in that there can be several options for each question, and the Answer table can store one of the QuestionOptions.

public class Question
{
    public int QuestionId { get; set; }
    public string QuestionText { get; set; }
    public bool Active { get; set; }
    public bool Required { get; set; }
    public bool AskToExplain { get; set; }
    public string TextToAskToExplain { get; set; } 

    public virtual int QuestionTypeId { get; set; }
    public virtual QuestionType QuestionType { get; set; }

    public virtual ICollection<QuestionOption> QuestionOptions { get; set; }

    public virtual ICollection<QuestionAnswer> QuestionAnswers { get; set; }
}

public class QuestionOption
{
    public int QuestionOptionId { get; set; }
    public string OptionText { get; set; }

    public virtual int QuestionId { get; set; }
    public virtual Question Question { get; set; }

    public virtual ICollection<QuestionAnswer> QuestionAnswers { get; set; }
}

public class QuestionAnswer
{
    public int QuestionAnswerId { get; set; }
    public string AnswerText { get; set; }

    public virtual int? QuestionOptionId { get; set; }
    public virtual QuestionOption? QuestionOption { get; set; }        

    public virtual int QuestionId { get; set; }
    public virtual Question Question { get; set; }

    public virtual int ProfileID { get; set; }
    public virtual Profile Profile { get; set; }
}

Inserting a row into QuestionAnswers table works fine, but updating throws this error:

The property 'QuestionOptionId' on entity type 'QuestionOption' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key first delete the dependent and invoke 'SaveChanges' then associate the dependent with the new principal.

    public QuestionAnswer SaveAnswer(QuestionAnswer answer)
    {
        var chosenOption = _db.QuestionOptions.Find(answer.QuestionOptionId);
        var profile = _db.Profiles.Find(answer.Profile.ProfileID);
        var question = _db.Questions.Find(answer.Question.QuestionId);

        if (answer.QuestionAnswerId == 0)
        {   //this works fine...
            var newAnswer = new QuestionAnswer()
            {
                AnswerText = answer.AnswerText,
                QuestionOption = chosenOption,
                Profile = profile,
                Question = question
            };

            var addedEntity = _db.QuestionAnswers.Add(newAnswer);
            _db.SaveChanges();
            return addedEntity.Entity;
        }
        else
        {
            // This is where the error happens...
            var existingAnswer = _db.QuestionAnswers.Find(answer.QuestionAnswerId);
            existingAnswer.AnswerText = answer.AnswerText;
            existingAnswer.QuestionOption = chosenOption;
            _db.SaveChanges(); //<-- Error thrown here
            return existingAnswer;
        }
    }

The error message sounds like I'm trying to change the primary key of the QuestionOption, but I'm actually just trying to change the QuestionOption that the QuestionAnswer points to. I just want it to basically run this SQL:

 UPDATE QuestionAnswer
 SET AnswerText = @NewAnswerText, QuestionOptionId = @NewQuestionOptionId
 WHERE QuestionAnswerId = @QuestionAnswerId

What am I doing wrong?

Thanks in advance for any help.

1

There are 1 answers

0
pjs On

No guarantee, but you could try setting the FK existingAnswer.QuestionOptionId = answer.QuestionOptionId instead of setting the related entity. That's what you would do in the database.