替换实体框架对象 [英] Replace Entity Framework object
问题描述
在MVC应用程序中,我有一个用于经典视图/编辑/创建模式的大对象。当用户编辑对象时,将其保存为:
public bool SetMyObject(MyObject newObject){
MyObject current = GetObjectById(newObject.Id);
current.Prop1 = newObject.Prop1
...
current.PropN = newObject.PropN
db.SaveChanges();
}
MyObject很大,所以我想知道有没有更好的方法来做,不涉及每个财产分配。例如, db.MyObject.UpdateObject(current,tnew)
。
Ty。
您可以使用 ApplyPropertyChanges
方法。
将分离对象的属性更改应用于已附加到对象上下文的对象。
public bool SetMyObject(MyObject newObject)
{
db.ApplyPropertyChanges(MyObjectSet,newObject);
db.SaveChanges();
}
(其中MyObjectSet是对象的实体集的名称属于)
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.
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)
这篇关于替换实体框架对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!