如何更新使用EF唯一的单场 [英] How to Update only single field using EF
问题描述
这是目前基本code:
This is the current basic code :
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Registration registration)
{
if (ModelState.IsValid)
{
db.Entry(registration).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(registration);
}
我在报名表中约15场,我只是想怎么过更新的日期字段,我这里收到的对象是登记,只具有价值的日期,但目前的code更新所有的项目,我想是刚刚更新的日期字段的值我登记已经得到
I have around 15 fields in Registration table , how ever i just want to update the "Date" field , the object that i receive here is "registration" which only has value for a date , but the current code updates all the entries , what i want is to just update the "Date" field , whose value i have got in "registration"
帮助将非常AP preciated:)
Help would be much appreciated :)
推荐答案
它安装在不变
状态的情况下,只有设置日期
进行修改。
Attach it to the context in the Unchanged
state, and only set Date
as modified.
if (ModelState.IsValid)
{
db.Registrations.Attach(registration); // attach in the Unchanged state
db.Entry(registration).Property(r => r.Date).IsModified = true;
// Date field is set to Modified (entity is now Modified as well)
db.SaveChanges();
return RedirectToAction("Index");
}
您说传入的实体只有日期
填写,希望有一个标识了。 :)
You said the incoming entity only has Date
filled in, hopefully there's an Id too. :)
这篇关于如何更新使用EF唯一的单场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!