locked
SavedChangesResult RRS feed

  • Question

  • Case: Creating related entities using OrganizationServiceContext.SaveChanges() method.

    Failure: SaveChangesResultCollection fails to populate when an exception is thrown by the SaveChanges() method

    I need to know what entities in the context got through to the DB and need to show error log to the user for those which failed. Currently once and exception is thrown the results variable is null. Shouldn't it be holding Saved Changes Results? Am I using it correctly?

     SaveChangesResultCollection results = null;
          try
          {
            results = context.SaveChanges(SaveChangesOptions.None);
          }
          catch (SaveChangesException ex)
          {
            if (results.HasError)
            {
    
              StringBuilder sb = new StringBuilder();
              foreach (var result in results)
              {
                if (result.Error != null)
                {
                  sb.AppendFormat("Error: {0}\n", result.Error.Message);
                }
              }
              throw new InvalidOperationException("Failure occurred while " +
                "calling OrganizationServiceContext.SaveChanges()\n" +
                sb.ToString());
            }
            else
            {
              foreach (Entity e in entities)
              {
                e.EntityState = null;
              }
            }
          }
    


    Manny Grewal || Australia
    Tuesday, June 21, 2011 7:55 AM

Answers

  • You are not using it correctly. If an exception is thrown within the SaveChanges call, then execution immediately flows into the catch block, and therefore the assignment to the variable "results" is never executed. This is the reason why the variable has its initial value of null.

    If you need futher information, I suggest posting the question in one of the forums related to software development, such as the one linked below for Entity Framework, since the question is not really appropriate for the Training and Certification forum:

    http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/threads/

     

    • Marked as answer by Manny Grewal Wednesday, June 22, 2011 1:14 AM
    • Unmarked as answer by Manny Grewal Wednesday, June 22, 2011 1:14 AM
    • Marked as answer by Rubel Khan Monday, July 25, 2011 12:48 AM
    Tuesday, June 21, 2011 8:14 AM

All replies

  • You are not using it correctly. If an exception is thrown within the SaveChanges call, then execution immediately flows into the catch block, and therefore the assignment to the variable "results" is never executed. This is the reason why the variable has its initial value of null.

    If you need futher information, I suggest posting the question in one of the forums related to software development, such as the one linked below for Entity Framework, since the question is not really appropriate for the Training and Certification forum:

    http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/threads/

     

    • Marked as answer by Manny Grewal Wednesday, June 22, 2011 1:14 AM
    • Unmarked as answer by Manny Grewal Wednesday, June 22, 2011 1:14 AM
    • Marked as answer by Rubel Khan Monday, July 25, 2011 12:48 AM
    Tuesday, June 21, 2011 8:14 AM
  • Thanks Alberto, thats helpful. I wanted to put this question in the CRM forum but was forced to put in this forum, because of a bug in this site which never shows options for other forums like Linq, Crm, .NET.
    Manny Grewal || Australia
    Wednesday, June 22, 2011 1:16 AM