Charkins12
Charkins12

Reputation: 190

Operation is not valid due to the current state of the object in C# Entity Framework

I have a method that takes in a form submission and submissions answers. In order to save the submission answers to the database; I need to set the submission answers with a reference ID to the submission.

I am attempting to do this but receiving the error listed in the title: "Operation is not valid due to the current state of the object" and I am unsure why.

I've tried to save everything to the database before adjusting any values; but that doesn't seem to help. I get another error saying that I am attempting to remove a foreign key.

I imagine this is a db context issue.

public FormSubmission AddSubmission(FormSubmission submission, List<FormSubmissionAnswer> submissionAnswers, int CustomerId, string CustomerName)
    {
        Form form = GetForm(submission.FormId.ToString());
        //set submission values
        submission.FormTitle = form.Title1;
        submission.DateSubmitted = DateTime.Now;
        if (CustomerId > 0)
            submission.PSMCustomerId = CustomerId;
        submission.CustomerName = CustomerName;
        base.SubscriptionDB.FormSubmissions.InsertOnSubmit(submission);
        base.SubscriptionDB.SubmitChanges();

        //need to update the submission answers FormSubmissionId            
        foreach (FormSubmissionAnswer answer in submissionAnswers)
            answer.FormSubmissionId = submission.Id; //erroring here

      base.SubscriptionDB.FormSubmissionAnswers.InsertAllOnSubmit(submissionAnswers);
        base.SubscriptionDB.SubmitChanges();

}

Upvotes: 2

Views: 4524

Answers (2)

Vinu
Vinu

Reputation: 36

Instead of

foreach (FormSubmissionAnswer answer in submissionAnswers)
            answer.FormSubmissionId = submission.Id; //erroring here

Try

foreach (FormSubmissionAnswer answer in submissionAnswers)
            submission.FormSubmissionAnswers.Add(answer);

In the end call base.SubscriptionDB.SubmitChanges();

base.SubscriptionDB.FormSubmissions.InsertOnSubmit(submission); base.SubscriptionDB.SubmitChanges(); //Remove from FormSubmission instead use at only one time. This should solve your problem.

Your final code should be like below

Form form = GetForm(submission.FormId.ToString());
        //set submission values
        submission.FormTitle = form.Title1;
        submission.DateSubmitted = DateTime.Now;
        if (CustomerId > 0)
            submission.PSMCustomerId = CustomerId;
        submission.CustomerName = CustomerName;
        base.SubscriptionDB.FormSubmissions.InsertOnSubmit(submission);
        
        //need to update the submission answers FormSubmissionId            
        foreach (FormSubmissionAnswer answer in submissionAnswers)
            submission.FormSubmissionAnswers.Add(answer); 
   
        base.SubscriptionDB.SubmitChanges();

I assume that you have proper entity relations.

Upvotes: 2

Ben Krueger
Ben Krueger

Reputation: 1536

What do your FormSubmission and FormSubmissionAnswer entities look like? I assume that FormSubmission has some kind of IEnumerable property to represent the relationship between the two. Try adding the answer objects to the list before doing the initial submit changes:

submission.CustomerName = CustomerName;
submission.FormSubmissionAnswers.AddRange(submissionAnswers);
base.SubscriptionDB.FormSubmissions.InsertOnSubmit(submission);
base.SubscriptionDB.SubmitChanges();

I believe EF should automatically generate the PK's and correctly assign the correct FK's as well. Then you wouldn't even need to do:

//need to update the submission answers FormSubmissionId            
foreach (FormSubmissionAnswer answer in submissionAnswers)
    answer.FormSubmissionId = submission.Id; //erroring here

base.SubscriptionDB.FormSubmissionAnswers.InsertAllOnSubmit(submissionAnswers);
base.SubscriptionDB.SubmitChanges();

Upvotes: 2

Related Questions