English 中文(简体)
B. 实体框架没有更新数据库中的数据并更新
原标题:Entity Framework not updating data in database with update

我试图将实体框架4.1从我MVC3控制器的Edit行动中插入/更新数据,其编码如下:

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

            UserProfile up = cpctx.UserProfiles.Where(w => w.UserName.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.

问题回答

暂无回答




相关问题
Using jquery to get a partial view and updating the UI

I need to render a partial view (returned from the controller) to show some customer summary details. This will need to happen when the user clicks on a button. In the the mean time the user can ...

MVC 2 / MVC 3 / MVC 4

MVC 2 我们可以轻松地创造领域。 现在,我的问题涉及nes地区(地区内)。

Asp.Net MVC 2 - Changing the PropertyValueRequired string

Using a resx file in the App_GlobalResources directory, I ve been able to change the default message for the PropertyValueInvalid string of the model validators. But it doesn t work to translate the ...

ASP.NET MVC 3 - What features do you want to see? [closed]

I know a bunch of people that are really enjoying the improvements that ASP.NET MVC 2 made over the first release. I have just started to migrate our MVC 1 project over and so far areas has totally ...

热门标签