Entity framework 使用实体框架持久化模型绑定对象
我会尽量保持简短 我这里有我的控制器Entity framework 使用实体框架持久化模型绑定对象,entity-framework,model-binding,Entity Framework,Model Binding,我会尽量保持简短 我这里有我的控制器 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(CustomObject myCustomObject) { ... } myCustomObject看起来很棒的地方。但是,如果我想用实体框架保存它,我需要这样做 [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(CustomObject myCustomObject) {
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
...
}
myCustomObject看起来很棒的地方。但是,如果我想用实体框架保存它,我需要这样做
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(CustomObject myCustomObject)
{
CustomObject existingObject = repository.GetCustomObject(myCustomObject.ID);
// Set all the attributes of myCustomObject to existingObject
existingObject.SomeMapperFunction(myCustomObject)
repository.Save();
}
有什么方法可以避免做这个映射练习吗
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id)
{
CustomObject existingObject = repository.GetCustomObject(id);
TryUpdateModel(existingObject);
// You additionaly can check the ModelState.IsValid here
repository.Save();
}