实体框架无法更新记录

时间:2016-12-02 10:34:21

标签: sql asp.net-mvc entity-framework model-view-controller entity

我正在使用MVC和Entity框架开发Web应用程序,我遇到了一个问题,现在开始变得很烦人。

        [HttpPost]
    [ValidateAntiForgeryToken]
    [ValidateInput(true)]
    public ActionResult ChangeUser(HttpPostedFileBase avatar, int? id)
    {
        try
        {
            if (id == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            if (ModelState.IsValid)
            {

                Models.Users usr = db.Users.Find(id);
                var at = db.Users.Attach(usr);
                if (usr == null) return HttpNotFound();
                at.Password = Encryption.Encode(usr.Password);
                at.Email = usr.Email;
                at.NickName = usr.NickName;
                if (avatar != null && avatar.ContentLength > 0)
                {
                    var _fileName = Path.GetFileName(avatar.FileName);
                    var ext = Path.GetExtension(_fileName);
                    var fileName = Encryption.EncodeFileName(_fileName);
                    //End of file properties
                    var path = Path.Combine(Server.MapPath("~/CMS-Content/User/Files/Avatars/"), fileName + ext);
                    avatar.SaveAs(Server.MapPath(path));
                    at.ThumbnailPath = path;
                }               
                db.Entry(usr).State = EntityState.Modified;
                // other changed properties
                db.SaveChanges();

                TempData["result"] = "User settings changed successfully.";
            }               
        }
        catch
        {
            TempData["result"] = "An error occured to change the user information!";
        }
        return RedirectToAction("Users");
    }

问题是数据库中的记录不会更新。我得到他们已成功更新的输出,但在数据库中记录不会改变。

0 个答案:

没有答案