locked
Save entity error when using ADO.NET Data Service RRS feed

  • Question

  • I tried to use following code to save data async:

              NorthwindEntities svcx = new   NorthwindEntities(new Uri("NorthwindDataService.svc", UriKind.Relative));                      
                try
                {  
                    svcx.UpdateObject(_people);    //at this line, get error
                    svcx.BeginSaveChanges(delegate(IAsyncResult result)
                    {
                        try
                        {
                            svcx.EndSaveChanges(result);                       
                        }
                        catch (DataServiceRequestException ex)
                        {
                            Debug.WriteLine("OnSaveChangesCompleted Error: " + ex.Response.ToString());
                        }

                    }, _poeple);
                }
                catch (DataServiceRequestException ex)
                {
                    Debug.WriteLine("OnUpdate Error: " + ex.Response.ToString());
                }

    But when the running went to the line  svcx.UpdateObject(_people); , I got error as below:

    An exception of type 'System.ArgumentException' occurred in System.Data.Services.Client but was not handled in user code

    Additional information: The context is not currently tracking the entity.

    What's wrong with it? how to fix it?
    Tuesday, December 23, 2008 4:26 PM

All replies

  • Hi,

    You need this line before the UpdateObject call :

    svcx.AttachTo("NameOfThEntitySet",_people);  


    When you call AttachTO , we start tracking the object for any changes to it .
    Since you are creating a new instance of the context, any state information of the Entities
    is lost and you have to recreate the states again.

    Phani Raj Astoria
    Tuesday, December 23, 2008 7:19 PM
    Moderator