如何将新对象添加到现有实体,PUT数据并将EF添加到我的数据库?

时间:2013-07-30 07:39:41

标签: c# asp.net-mvc json entity-framework asp.net-mvc-4

我正在使用EF5,SQL Server 2012和Web前端。在服务器上,我有以下两个类:

public class Question
{
    public Question()
    {
        this.Answers = new List<Answer>();
    }
    public int QuestionId { get; set; }
    public string Title { get; set; }
    public string Answer { get; set; }
    public virtual ICollection<Answer> Answers { get; set; }

}
public class Answer
{
    public int AnswerId { get; set; }
    public string Text { get; set; }
    public int QuestionId { get; set; }
    public virtual Question Question { get; set; }
}

我向我的前端应用程序检索了一个问题,添加了一个空的答案:

   var emptyAnswer = { "answerId"; null, "text": "xx", "questionId": formData.questionId};
   formData.answers.push(emptyAnswer);

然后我对服务器进行了PUT并注意到当它进入服务器时,answerId被正确设置为null。

    public HttpResponseMessage PutQuestion(int id, Question question)
    {
        var a = question;
    }

我的ASP.NET WebApi控制器接收数据但是当我检查新创建的答案对象时,它现在是我的问题的一部分,它的AnswerId为 0

任何人都可以告诉我为什么它会从null更改&gt; 0?问题是,当我现在做一个更新实体 框架给了我一个错误,因为它期望新创建的答案没有它的AnswerId设置。

请注意。我在对象进入Entity Framework之前进行检查。我提到我正在使用它但是在这一点上我不认为它已经进入图片了,因为我只是检查了poco类。下面有一些关于该领域不可空的内容。但是,当我创建一个新对象时,有效负载是相似的,例如当我创建一个“内容”对象时,我将该字段设置为null并保持为null。

到目前为止,这是我测试的结果。我把前端带出了循环,所以一切都更清晰了:

使用:

var a = new Answer{
    Correct = false,
    Response = false,
    Text = "AA",
    Image = "DDD",
    QuestionId = question.QuestionId
};
question.Answers.Add(a);
_uow.Questions.Update(question);
_uow.Commit();

不起作用:ObjectStateManager中已存在具有相同键的对象。 ObjectStateManager无法使用相同的键跟踪多个对象。

var a = new Answer{
    AnswerId = 0,
    Correct = false,
    Response = false,
    Text = "AAA",
    Image = "DDD",
    QuestionId = question.QuestionId
};
var b = new Answer
{
    AnswerId = 0,
    Correct = false,
    Response = false,
    Text = "BBB",
    Image = "EEE",
    QuestionId = question.QuestionId
};

无效。它不会创建新的实体值。它只使用1000和1001

var a = new Answer{
    AnswerId = 1000,
    Correct = false,
    Response = false,
    Text = "AAA",
    Image = "DDD",
    QuestionId = question.QuestionId
};
var b = new Answer
{
    AnswerId = 1001,
    Correct = false,
    Response = false,
    Text = "BBB",
    Image = "EEE",
    QuestionId = question.QuestionId
};

不起作用:编译器错误。无法将null转换为int

var a = new Answer{
    AnswerId = null,
    Correct = false,
    Response = false,
    Text = "AAA",
    Image = "DDD",
    QuestionId = question.QuestionId
};
var b = new Answer
{
    AnswerId = null,
    Correct = false,
    Response = false,
    Text = "BBB",
    Image = "EEE",
    QuestionId = question.QuestionId
};

不起作用:ObjectStateManager无法使用相同的键跟踪多个对象。

var a = new Answer{
    Correct = false,
    Response = false,
    Text = "AAA",
    Image = "DDD",
    QuestionId = question.QuestionId
};
var b = new Answer
{
    Correct = false,
    Response = false,
    Text = "BBB",
    Image = "EEE",
    QuestionId = question.QuestionId
};

1 个答案:

答案 0 :(得分:1)

这是因为Answer上的AnswerId属性不可为空

public class Answer
{
    public int AnswerId { get; set; }
    public string Text { get; set; }
    public int QuestionId { get; set; }
    public virtual Question Question { get; set; }
}

此外,EF显然认为您将设置适当的ID。如果AnswerId被配置为标识列(自动递增id),则EF不会抱怨。