English 中文(简体)
Can System. Web.MVC.Update Model Update EF Navigation Property?
原标题:
  • 时间:2009-05-19 22:04:44
  •  标签:

如果我在数据库中有2个表格:Foobar。 <<>Foo通过FooIdBar通过<>BarId识别。 。 因此,Foo有BarId作为外国钥匙。

我的模型体现了这一点,一种看法可用于选择相关的Bar。

鉴于控制器有以下方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formCollection)
{
    Foo originalFoo = FooById(id);

    if (!ModelState.IsValid)
    {
        return View(new VenueViewModel(originalVenue, _db.GetCounties(), _db.VenueTypeSet));
    }

    UpdateModel(originalFoo);

    /* Instead of using UpdateModel I could just iterate through 
       formCollection and manually update originalFoo, it would 
       work but surely there is a better way? */

    _db.SaveChanges();

    return RedirectToAction("Index");
}

电话UpdateModelInvalidOperationException<>>寄发<>InnerException:

The model of type  TestApplication.Models.Foo  was not successfully updated.

我认为,我的控制者从下降中更新基于实体框架的模式,有什么正确方法?

最佳回答

无,缺省模式将无法做到这一点的 is子捆起来。 很难说,你要写一种习惯模式,必须这样做。 超文本数字电子数据元件只能储存描述中的识别值,不足以使大多数实体的情况发生。 因此,我们只需要一个身份证,不足以使一个实体投入工作,而没有打上数据库。

因此,在更新实体的导航特性时,我们有两个选择:

  1. Read in the entity from the database and assign it to the navigation property in the controller.
  2. Don t attempt to materialize an entity for the navigation property; instead just assign the EntityKey.

后者是我所做的。 你们可以在约束器或控制器的习俗模式中这样做。 你可以读得很多,在联系上读得更多,我在这方面的评论中有一些样本。

即将推出的.NET 4.0中的Entity Framework的新版本将拥有一个名为"FK Associations"的新功能,这将使这个过程变得更容易。

问题回答

暂无回答




相关问题
热门标签