Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Entity framework 使用实体框架持久化模型绑定对象_Entity Framework_Model Binding - Fatal编程技术网

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();
}