如何使用ObjectContext的执行更新 [英] How to perform an update using ObjectContext

查看:148
本文介绍了如何使用ObjectContext的执行更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的ASP.NET MVC 2 Web应用程序EF4。我试图将数据保存到数据库中。这是相当简单的添加的新的数据到数据库。我做的是这样的:

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

但我收到此InvalidOperationException异常:

but I am receiving this InvalidOperationException:

与相匹配的一个关键的对象
  所提供的对象的密钥无法
  在ObjectStateManager找到。
  验证的键值
  提供对象匹配的键值
  对象到修改必须
  应用。

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来问:

我试过下面提供的答案,但是当我去调试语句,我发现我被拯救的人的I​​D总是0 - 即使我从数据库中检索它。所以,我想后我在做什么,以重新获得首位的Web应用程序。也许这会有点帮助。

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://本地主机/人/索引/ 1

其中1是在数据库中的人的ID。

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

现在,我在看,我不知道它是错的实例化一个地方实体对象我想要检索一个新的人每次。 (现在要研究的。)在任何情况下,当我这样做,最终去节省超过previous人,我发现是PersonID值总是== 0。

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.

推荐答案

entities.SaveChanges(); 是所有你需要,只要你在完全相同的检索的对象上下文。否则,你需要 entities.Attach通话(人);

entities.SaveChanges(); is all you need, provided you retrieved the object from the same exact context. Otherwise, you'll need a call to entities.Attach(person);.

这应该工作:

PeopleEntities entities = ObjectContextFactory.GetPeopleEntities();
var person  = entities.People.Single(p => p.Id == id); // or some other way to get the person in question
// make changes to person ...
entities.SaveChanges();

这篇关于如何使用ObjectContext的执行更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆