由于C#实体框架中对象的当前状态,因此操作无效

时间:2019-02-07 17:24:19

标签: c# entity-framework

我有一种方法可以接受表单提交和提交答案。 为了将提交的答案保存到数据库中;我需要使用提交的参考ID设置提交答案。

我正在尝试执行此操作,但收到标题中列出的错误: “由于对象的当前状态,操作无效”,我不确定为什么。

在调整任何值之前,我尝试将所有内容保存到数据库;但这似乎无济于事。我收到另一个错误消息,说我正在尝试删除外键。

我想这是数据库上下文问题。

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

}

2 个答案:

答案 0 :(得分:2)

您的FormSubmission和FormSubmissionAnswer实体是什么样的?我假设FormSubmission具有某种IEnumerable属性来表示两者之间的关系。在进行初始提交更改之前,尝试将答案对象添加到列表中:

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

我相信EF应该自动生成PK并正确分配正确的FK。然后,您甚至都不需要这样做:

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

答案 1 :(得分:2)

代替

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

尝试

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

在结束通话中 base.SubscriptionDB.SubmitChanges();

base.SubscriptionDB.FormSubmissions.InsertOnSubmit(submission);         base.SubscriptionDB.SubmitChanges(); //从FormSubmission删除,而只能使用一次。这应该可以解决您的问题。

您的最终代码应如下所示

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

我假设您具有适当的实体关系。