开发者

Entity Framework not updating data in database with update

开发者 https://www.devze.com 2023-04-10 11:12 出处:网络
I am trying to insert/update data into a table using Entity Framework 4.1 from an Edit action in my MVC3 controller with the following code:

I am trying to insert/update data into a table using Entity Framework 4.1 from an Edit action in my MVC3 controller with the following code:

[HttpPost]
public ActionResult Edit(UserDetailsForm form)
{
    try
    {
        if (ModelState.IsValid)
        {

            UserProfile up = cpctx.UserProfiles.Where(w => w.UserNam开发者_运维知识库e.Equals(form.email)).SingleOrDefault();
            if (up == null)
            {
                up = new UserProfile();
                up.UserName = form.email;
                up.FirstName = form.FirstName;
                up.LastName = form.LastName; 
                ctx.UserProfiles.Add(up);
            }
            else
            {
                up.FirstName = form.FirstName;
                up.LastName = form.LastName; 
                cpctx.Entry(up).State = EntityState.Modified;
            }



            ctx.SaveChanges();

            return RedirectToAction("Index");
        }

        return View(form);
    }
    catch
    {
        return View(form);
    }
}

Everything works fine for an new record and I get a new row in the database, my problem occurs when updating. The code executes without any exceptions and I can see the values in up correctly in the watch window, however, when ctx.SavesChanges() is executed the database is not updated.

Any help in resolving this issue will be greatly appreciated.

0

精彩评论

暂无评论...
验证码 换一张
取 消