强大的ASP.NET MVC与实体框架 [英] Strongly-Typed ASP.NET MVC with Entity Framework

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

问题描述

此代码无法保存任何更改:

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 分离的值

ASP.NET MVC creates the object Model as a Department type EntityObject with an EntityState value of Detached.

使用 AttachTo 方法后,其EntityState变为不变

After using the AttachTo method, its EntityState becomes Unchanged.

MSDN on附加对象(实体框架)


对象附加到对象
上下文中不变状态。

由于不变状态,方法 ApplyPropertyChanges 不做任何事情

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

我希望它的状态为修改

实体统计枚举的MSDN


已分离

对象存在,但没有被Object
服务跟踪。一个实体在创建
之后立即处于这种状态
,之后它被添加到对象
上下文中。一个实体也是在
状态之后,通过调用Detach
方法从
的上下文中删除,或者使用
NoTrackingMergeOption加载。

Detached
The object exists but it is not being tracked by Object Services. An entity is in this state immediately after it has been created and before it is added to the object context. An entity is also in this state after it has been removed from the context by calling the Detach method or if it is loaded using a 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.

使用EntityObjects强制类型的MVC控制器是不可能的吗?

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,EF会将整个实体视为已修改,并将每个属性都保留到数据存储。使用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 v1.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与实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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