How to Update only single field using EF

后端 未结 3 1442
傲寒
傲寒 2020-12-18 15:55

This is the current basic code :

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Registration registration)
    {
        if (Mode         


        
3条回答
  •  执笔经年
    2020-12-18 16:19

    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. :)

提交回复
热议问题