English 中文(简体)
A. 试图利用实体框架更新实体的错误 4
原标题:Error when trying to update entity using Entity Framework 4

我有以下Edit方法:

[HttpPost]
public ActionResult Edit(Movie movie)
{
    try
    {
        _db.ApplyCurrentValues("Movies1",movie);
        _db.SaveChanges();
        return RedirectToAction("Index"); 
     }
     catch
     {
         return View();
     }
 }

我的错误如下:

<编码> 标语中找不到与所提供物体的关键相匹配的物体。 证明所供应物体的关键价值与必须加以修改的物体的关键价值相符。

两点:

  • I did not get the error the first time I did an update, only subsequent updates.
  • Movies1 is the the name of the EntitySet when I view the edmx designer. Is this what it shold be or should it be the name of the table (Movie)?
  • I have seen things regarding Attach, but I am confused as to what exactly that is.
最佳回答

要求使用现值的实体应当出现在<代码>中。 ObjectStateManager。 http://msdn.microsoft.com/en-us/library/d487246.aspx” rel=“nofollow” ApplyCurrentValues

Copies the scalar values from the supplied object into the object in the ObjectContext that has the same key.

您可以附上该实体,并适用现值。

    _db.Movies.Attach(movie);
    _db.ObjectStateManager.ChangeState(movie, EntityState.Modified);
    _db.SaveChanges();
问题回答

暂无回答




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签