2

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 my controller.

When I am calling the ApplyPropertyChanges method, I am getting an 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:

[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;
}
1
  • I explained the same problem in slightly different way here: stackoverflow.com/questions/2443062/… The main reason is that there is a TypeConverter that ends up adding State (which is an entity / lookup table) to the address, and that moves Address object from Detached to Added state. And now it is impossible to ApplyPropertyChanges or Attach the wrongfully added Address. It works fine if, instead of using TypeConverter, one assigns EntityReference in the controller itself. so - is it possible to avoid adding the object? Commented Mar 16, 2010 at 2:18

4 Answers 4

1

The error is the object is detached from the context, and ApplyPropertyChanges thinks the object is added because it isn't attached. So you would need to query from the data context or get an attached form and then apply the changes then.

HTH.

Sign up to request clarification or add additional context in comments.

Comments

1

What Dave Said

+

You need to Attach() the disconnected entity to your object context:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.attach.aspx

miEntity.Attach(regUserAddress);
miEntity.SaveChanges();

2 Comments

Are you sure the record is in the database? What is AD_id? The PK?
Yes. There is this record in the database.
0

Just add the following code before miEntity.SaveChanges():

miEntity.Entry(regUserAddress).State = EntityState.Modified;

Comments

0

First select the record (object entity), search by key through the ObjectContext. For example if the search ArticleSet EntitySet called for there to record, and once you get it modified its properties with new values and then call SaveChanges() of ObjectContext.

Example:

ObjectQuery<Article> myArt=Context.ArticleSet.Where myArt = (row => row.ArticleId == value);
myArt.Description=" new value ";
etc. ..
etc ...

Context.SaveChanges ();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.