如何使用 ObjectContext 执行更新
我在 ASP.NET MVC 2 Web 应用程序中使用 EF4。我正在尝试将数据保存到数据库中。将新数据添加到数据库相当简单。我正在这样做:
PeopleEntities entities = ObjectContextFactory.GetPeopleEntities();
entities.People.AddObject(person); // The Person object created from user input.
entities.SaveChanges();
但是,这有一个不幸的副作用,即总是将新对象保存到我的数据库中。我确实需要此功能(当然),但我还希望能够选择更新数据库中的现有项目(或者,如果数据库中尚不存在该项目,则另存为新项目)。
我尝试用这样的内容替换上面的第二行:
entities.People.ApplyCurrentValues(person);
但我收到了这个 InvalidOperationException:
一个对象,其键与 所提供对象的密钥无法 可以在 ObjectStateManager 中找到。 验证的关键值 提供的对象与键值匹配 必须进行更改的对象的 已应用。
无论我尝试添加新人员还是尝试编辑人员,都会发生这种情况。我可以做什么来纠正这个问题?
问题更新:
我尝试了下面提供的答案,但是当我去调试 Single
语句时,我发现我保存的 Person 的 ID 始终为 0 -即使我已经从数据库中检索了它。因此,我想首先发布我正在做什么来检索 Web 应用程序。也许这会有所帮助。
当我导航到显示人员信息的页面时,地址为:
http://localhost/Person/Index/1< /a>
其中 1 是数据库中人员的 ID。我通过包含以下行的存储库方法检索 Person(实体是存储库类的成员变量):
public Person GetPerson(int id)
{
return entities.People.FirstOrDefault(p => p.PersonID == id);
}
此方法是从我的项目的控制器调用的。看起来像这样:
public ActionResult Index(int id)
{
var entities = ObjectContextFactory.GetScenarioDBEntities();
Person person = new PersonRepository(entities).GetPerson(id);
return View("Index", person);
}
现在我正在考虑这个问题,我想知道每次我想要检索一个新的 Person 时实例化一个本地实体对象是否是错误的。 (现在要研究一下。)无论如何,当我这样做时,最终去保存前一个 Person,我发现 PersonID 值始终 == 0。
I am using EF4 in my ASP.NET MVC 2 web application. I am attempting to save data to a database. It is fairly straightforward to add new data to a database. I'm doing it something like this:
PeopleEntities entities = ObjectContextFactory.GetPeopleEntities();
entities.People.AddObject(person); // The Person object created from user input.
entities.SaveChanges();
However, this has the unfortunate side-effect of always saving a new object to my database. I do need this functionality (of course), but I also want the option to update an existing item in the database (or, save as new if the item does not yet exist in the database).
I tried replacing the second line above with something like this:
entities.People.ApplyCurrentValues(person);
but I am receiving this InvalidOperationException:
An object with a key that matches the
key of the supplied object could not
be found in the ObjectStateManager.
Verify that the key values of the
supplied object match the key values
of the object to which changes must be
applied.
This case happens whether I try to add a new person, or if I am trying to edit a person. What can I do to correct this issue?
Update to Question:
I tried the answers provided below, but when I went to debug the Single
statement, I found that the ID of the Person I was saving was always 0 - even if I had retrieved it from the database. So, I wanted to post what I am doing to retrieve the web application in the first place. Maybe this will help a bit.
When I navigate to a page displaying a Person's information, the address is:
http://localhost/Person/Index/1
where 1 is the ID of the Person in the database. I retrieve the Person through a repository method that contains the following line (entities is a member variable of the repository class):
public Person GetPerson(int id)
{
return entities.People.FirstOrDefault(p => p.PersonID == id);
}
This method is called from my project's controller. That looks like this:
public ActionResult Index(int id)
{
var entities = ObjectContextFactory.GetScenarioDBEntities();
Person person = new PersonRepository(entities).GetPerson(id);
return View("Index", person);
}
Now that I am looking at that, I wonder if it's wrong to instantiate a local entities object every time I want to retrieve a new Person. (Going to research that now.) In any case, when I do, ultimately go to save over the previous Person, I find that the PersonID value is always == 0.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只要您从相同的上下文中检索对象,
entities.SaveChanges();
就是您所需要的。否则,您需要调用entities.Attach(person);
。这应该有效:
entities.SaveChanges();
is all you need, provided you retrieved the object from the same exact context. Otherwise, you'll need a call toentities.Attach(person);
.This should work:
试试这个:
Try this: