Reputation: 9046
In MVC app, I am having this big object that is used in classic view/edit/create pattern.
When user edits the object I save it as:
public bool SetMyObject(MyObject newObject) {
MyObject current = GetObjectById(newObject.Id);
current.Prop1 = newObject.Prop1
...
current.PropN = newObject.PropN
db.SaveChanges();
}
MyObject is pretty big so I am wondering is there any better way to do this, not involving per-property assignments. For instance something along the lines db.MyObject.UpdateObject(current, tnew)
.
Ty.
Upvotes: 1
Views: 1506
Reputation: 292615
You can use the ApplyPropertyChanges
method.
Applies property changes from a detached object to an object already attached to the object context.
public bool SetMyObject(MyObject newObject)
{
db.ApplyPropertyChanges("MyObjectSet", newObject);
db.SaveChanges();
}
(where "MyObjectSet" is the name of the entity set to which the object belongs)
Upvotes: 3