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 creates the object Model as a Department type EntityObject with an EntityState value of Detached.
After using the AttachTo method, its EntityState becomes Unchanged.
MSDN on Attaching Objects (Entity Framework)
Objects are attached to the object
context in an Unchanged state.
Because of its Unchanged state, the method ApplyPropertyChanges does nothing.
I want it to instead have state Modified.
MSDN on EntityState Enumeration
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.
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.
Modified
The object is changed but the SaveChanges method has not
been called.
I cannot explicitly set an EntityObject's EntityState property to Modified. It is read only.
Is it just impossible to have strongly-typed MVC controllers with EntityObjects?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…