locked
After RTM: Delete Child Entity RRS feed

  • Question

  •  

    Hi,

    Can you pls describe how to delete a object which has a Foreign key? Let me describe the scenario

    I have a User with multiple UserProfile so UserProfile has a FK for User. To add a new User profile i write the following code

     

    UserProfile profile = new UserProfile();

    profile.UserIdentity = user;

    entityStore.AddObject("UserProfile", profile);

    profile.UserIdentity.UserProfile.Add(profile);

    entityStore.AddLink(profile.UserIdentity, "UserProfile", profile);

    entityStore.BeginSaveChanges(SaveChangesOptions.Batch, delegate(IAsyncResult asyncResult)

                   { entityStore.EndSaveChanges(asyncResult); }, null);

     

    This works and a new Profile is added.

     

    Now i want to delete this profile.

     

    In beta the following code worked

                   var profile = entityStore.UserProfile.Expand("UserIdentity").Where(p => p.profileid == profileid);

                   UserProfile userProfile = profile.First<UserProfile>();

                   entityStore.DeleteObject(profile.First<UserProfile>());

                   entityStore.DetachLink(profile, "UserIdentity", profile.First<UserProfile>().UserIdentity);

                   entityStore.BeginSaveChanges(SaveChangesOptions.Batch, delegate(IAsyncResult asyncResult)

                   { entityStore.EndSaveChanges(asyncResult); }, null);

    But this doesn't work now. I've tried with DeleteLink and also with

    entityStore.DetachLink(userProfile.UserIdentity, "UserProfile", userProfile) but no luck ...all i get is "There was an error processing this request..

    I cud not find a sample in the RTM traning kit for this scenaro either. Please help here..

    Thanks

    Anshulee

    Thursday, August 14, 2008 1:52 AM

Answers

  • Code Snippet

      //Load the first Parent, change this query to suit your needs
      UserProfile firstuserProfile = entityStore.UserProfile.First<UserProfile>();
      //Load all the children for the parent entity
      entityStore.LoadProperty(firstuserProfile, "UserIdentity");
      //Detach all the child Entities , 
      foreach (UserIdentity uIdentity in firstuserProfile.UserIdentity)
      {
          entityStore.DetachLink(firstuserProfile, "UserIdentity", uIdentity);
          //Delete the child entity
          entityStore.DeleteObject(uIdentity);
      }

      AsyncCallback acSaveChanges = iasyncResult =>
      {
          try
          {
        DataServiceContext ctx = iasyncResult.AsyncState as DataServiceContext;
        DataServiceResponse dsResponse = ctx.EndSaveChanges(asyncResult);
        //Enumerate all the changes to see if there is an error
        foreach (ChangeSetOperationResponse cor in dsResponse)
        {
            //If there is an error , throw the error
            if (cor.Error != null)
            {
          throw cor.Error;
            }
        }

          }
          catch (Exception exception)
          {
        if (exception.InnerException != null)
        {
            throw (exception.InnerException);
        }
        else
        {
            throw exception;
        }
          }
      };

      entityStore.BeginSaveChanges(SaveChangesOptions.Batch, acSaveChanges, entityStore);

     

     

    Try this and let me know if you have any issues.

    Thursday, August 14, 2008 2:39 AM
    Moderator