Error while updating Database record with Entity Framework on ASP.NET MVC Page

Posted by Rupa on Stack Overflow See other posts from Stack Overflow or by Rupa
Published on 2010-03-12T19:43:29Z Indexed on 2010/03/12 19:47 UTC
Read the original article Hit count: 299

Hi

I have an ASP.NET Page that updates registered User Address Details for a selected record. Below is the Update method that i am calling from Controller.

When i am calling ApplyPropertyChanges method, I am getting the below error. Did anyone run into the same error while updating the record with Entity Framework.

Appreciate your responses.

Error Message:

The existing object in the ObjectContext is in the Added state. Changes can only be applied when the existing object is in an unchanged or modified state.

My Update Method Code:

[HttpPost]
    public bool UpdateAddressDetail([Bind(Prefix = "RegUser")] AddressDetail regUserAddress, 
                        FormCollection formData)
    {
    regUserAddress.AD_Id = 3;
        regUserAddress.LastUpdated = HttpContext.User.Identity.Name;
        regUserAddress.UpdatedOn = DateTime.Now;
        regUserAddress.AddressType = ((AddressDetail)Session["CurrentAddress"]).AddressType ?? "Primary";
        regUserAddress.Phone = ((AddressDetail)Session["CurrentAddress"]).Phone;
        regUserAddress.Country = ((AddressDetail)Session["CurrentAddress"]).AddressType ?? "USA";

        miEntity.ApplyPropertyChanges(regUserAddress.EntityKey.EntitySetName, regUserAddress);
        miEntity.SaveChanges();
        return true;
    }

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about entity-framework