本文介绍了强类型ASP.NET与实体框架MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code没有实际保存任何更改:

This code fails to actually save any changes:

//
// POST: /SomeType/Edit/5

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, SomeType Model)
{
    db.AttachTo(Model.GetType().Name, Model);
    db.ApplyPropertyChanges(Model.EntityKey.EntitySetName, Model);
    db.SaveChanges();
    return RedirectToAction("Index");
}

ASP.NET MVC创建对象模型作为一个部门类型EntityObject用的的EntityState值独立屋

使用后的 AttachTo 的方法,它的EntityState成为不变

After using the AttachTo method, its EntityState becomes Unchanged.

上附加对象(实体框架)

的对象被附连到对象  上下文在不变状态。

,该方法的 ApplyPropertyChanges 的什么也不做。

Because of its Unchanged state, the method ApplyPropertyChanges does nothing.

我想它,而不是有状态的修改

I want it to instead have state Modified.

rel="nofollow">

MSDN on EntityState Enumeration

独立屋
  存在的对象,但它不被跟踪的对象  服务。一个实体是在这种状态下  它已被创建之后立即  和之前被添加到对象  上下文。实体也正是在这种  之后,国家已经从被删除  通过调用分离的情况下  方法,或者如果它被加载用  NoTrackingMergeOption。

不变
  该对象未被修改,因为它被装入  上下文或自最后一次  该SaveChanges方法是  调用。

Unchanged
The object has not been modified since it was loaded into the context or since the last time that the SaveChanges method was called.

修改
  该对象被改变,但SaveChanges方法还没有  被调用。

Modified
The object is changed but the SaveChanges method has not been called.

我不能明确的EntityObject的的EntityState属性设置为修改。它是只读的。

I cannot explicitly set an EntityObject's EntityState property to Modified. It is read only.

难道只是不可能有强类型MVC控制器与EntityObjects?

Is it just impossible to have strongly-typed MVC controllers with EntityObjects?

推荐答案

您需要从ObjectContext中获得ObjectStateManager。随着ObjectStateManager,您可以明确地设置状态,你的对象,而无需拨打电话到数据库:

You need to get the ObjectStateManager from your ObjectContext. With the ObjectStateManager, you can explicitly set the state for your object without needing to make a call to the database:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Guid id, SomeType Model)
{
    db.AttachTo(Model.GetType().Name, Model);

    ObjectStateManager stateMgr = db.ObjectStateManager;
    ObjectStateEntry stateEntry = stateMgr.GetObjectStateEntry(model);
    stateEntry.SetModified(); // Make sure the entity is marked as modified
    //db.ApplyPropertyChanges(Model.EntityKey.EntitySetName, Model);

    db.SaveChanges();
    return RedirectToAction("Index");
}

该ObjectStateEntry,您还可以通过SetModifiedProperty应用更细粒度的状态变化数据。如果你打电话SetModified之,英孚将视为修改整个实体,并坚持每个属性的数据存储。随着SetModifiedProperty,EF可以优化查询,并只涉及已实际更改的属性。使用SetModifiedProperty显然更为复杂,因为你通常需要知道每个属性的初始值。

The ObjectStateEntry also allows you to apply finer-grained state change data via the SetModifiedProperty. If you call SetModified, EF will treat the entire entity as modified, and persist every property to the data store. With SetModifiedProperty, EF can optimize the queries and only involve the properties that have actually changed. Using SetModifiedProperty is obviously more complex, as you usually need to know the original value of each property.

我希望这有助于。 ObjectStateManager是在EF工具箱中一个强大的小工具,可以帮助改善EF 1.0版的,否则病态的性能和效率。

I hope this helps. ObjectStateManager is a powerful little tool in the EF toolbox, and can help improve EF v1.0's otherwise morbid performance and efficiency.

这篇关于强类型ASP.NET与实体框架MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!