entity-framework – 如何使用EF更新控制器中的对象?
我在谷歌搜索,但它似乎太明显,没有人在谈论这个.
我有一个我的表的存储库,我希望能够更新我的数据库. 在Linq2Sql上你有类似的东西: public void SaveCar(Car car) { if (carTable.GetOriginalEntityState(car) == null) { carTable.Attach(product); carTable.Context.Refresh(RefreshMode.KeepCurrentValues,car); } carTable.ContextSubmitChanges(); } 在控制器中,只需在POST Edit方法上调用此方法即可. 我如何在EF中做这样的事情?更好的方法. 我看到使用TryUpdateModel(模型)的代码,但我不确定是否更新我在数据库上的内容,或者我必须首先选择对象并使用FormCollection更新它… 我很困惑,我只需要从表单中获取一辆汽车并使用相同的ID从数据库更新汽车.那么,我需要在控制器和存储库中做些什么呢? 谢谢. 编辑:如果我不清楚,我真的不知道我放在那里是否需要转换为EF.我只是想知道如何使用EF更新对象的实例(EFCodeFirst是我正在使用的).我如何从表单接收实例并在数据库中更新它. 解决方法
TryUpdateModel方法可用于为对象分配值,使用其属性名称与Web请求中指定的值之间的映射.话虽这么说,你可以通过做这样的事情来实现相同的行为:
[HttpPost] public ActionResult Edit(int id,FormCollection form) { Car entity = new Car { Id = id }; // This will attach the car entity to the content in the unchanged state. this.EntityFrameworkObjectContext.Cars.Attach(car); TryUpdateModel(entity,form.ValueProvider.ToXYZ()); // Can't remember the exact method signature right now this.EntityFrameworkObjectContext.SaveChanges(); ... } 将实体附加到上下文时,您基本上只是通知上下文您有一个实体,他应该注意这个实体.一旦附加了实体,上下文将跟踪对其所做的更改. 但是此方法仅适用于标量属性,因为不使用此方法更新导航属性.如果启用了外键属性(如分配给类别的产品也具有链接两者的CategoryId属性),您仍应该能够使用此方法(因为导航属性使用标量属性进行映射). 编辑:另一种方法是接受Car实例作为参数: [HttpPost] public ActionResult Edit(int id,Car car) { Car entity = new Car { Id = id }; // This will attach the car entity to the content in the unchanged state. this.EntityFrameworkObjectContext.Cars.Attach(entity); // It is important that the Car instance provided in the car parameter has the // the correct ID set. Since the ApplyCurrentValues will look for any Car in the // context that has the specified primary key of the entity supplied to the ApplyCurrentValues // method and apply it's values to those entity instance with the same ID (this // includes the previously attached entity). this.EntityFrameworkObjectContext.Cars.ApplyCurrentValues(car); this.EntityFrameworkObjectContext.SaveChanges(); ... } 您还可以滚动自己的ModelBinder,它实际上具有对您的Entity Framework Context的引用,并查看是否在表单/查询中指定了Car.Id.如果存在ID,您可以直接从上下文中获取要更新的实体.此方法需要一些努力,因为您必须首先确保搜索ID,然后应用所有指定的属性值.如果你有兴趣,我可以给你一些考试. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何在Windows 10 / IIS 10上附加到IIS进程(w3w
- asp.net-mvc – 对MVC中的模型进行条件验证
- ASP.NET MVC4会话状态存储用户名
- asp.net repeater手写分页实例代码
- asp.net – nhibernate配置和buildsessionfactory时间
- 如何在Classic ASP中将数据发布到Web中的任何页面?
- asp.net-mvc – 将azure MVC应用程序置于维护模式的巧妙方法
- asp.net – HTTP错误400 – 与Topshelf的OWIN
- asp.net-mvc – 必需属性“pageBaseType”未找到 – 启动我
- asp.net – 用于查询字符串的基本身份验证的IIS重写规则
- ASP.NET——验证控件
- asp.net-mvc – 如何访问querystring在ASP.Net M
- asp.net-mvc – ASP.NET MVC日历
- asp.net-mvc – ASP.Net Core中的动态路由
- asp.net – 自动将视频格式转换为Flash Video
- asp.net-mvc – localhost拒绝在visual studio中
- asp.net-core – ASP.NET 5中的HandleUnknownAct
- asp.net-mvc – 有一个强大的方法来删除ASP.NET
- asp.net access web.config denied
- MVC中TempData、ViewData和ViewBag的区别