none
How can the details of an exception generated by DbContext.SaveChanges() be retrieved? RRS feed

  • Question

  • An exception generated by DbContext.SaveChanges() cannot be caught by DbEntityValidationException, EntityException or DataException.  It can be caught by  Exception, but the debugger gives the following for the instance of Exception:

    [System.InvalidOperationException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

    The code is the following:

                                        try
                                        {
                                            context.SaveChanges();
                                        }
                                        catch (DbEntityValidationException dbEx)
                                        {
                                        }
                                        catch (EntityException ee)
                                        {
                                        }
                                        catch (DataException de)
                                        {
                                        }
                                        catch (Exception ex)
                                        {
                                        }

    The code in the above catch blocks have been ommited.

    Any tip will be greatly appreciated.


    Hong

    Tuesday, January 1, 2013 5:01 PM

Answers

  • Hallelujah! The background thread is the issue.  There was no exception when I brought the function to a foreground thread.

    IIS kills background threads after Idle Time-out. Apparently, the debugger of VS cannot handle this situation.  I increased the Idle Time-out of the Application Pool used by the web app, and everything is fine now.

    It would be nice if a caught exception could tell what is going on

     

    Hong

    • Proposed as answer by Alexander Sun Thursday, January 3, 2013 7:26 AM
    • Marked as answer by Hong (MA, USA) Thursday, January 3, 2013 11:13 AM
    Tuesday, January 1, 2013 11:07 PM

All replies

  • Hi Hong;

    It sounds like you may be in Release mode. Try the following in the IDE main menu click on Build -> Configuration Manager... when the dialog box opens make sure that the combo box Active solution configuration is set to Debug. Then compile and try again.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 1, 2013 5:43 PM
  • Thanks for your prompt response, Fernando!  Happy New Year!

    I have been using the debug mode. Normally, I can get the details of exceptions without any problem.

    This is an MVC web application.  I can break at any line of code, and step through the program without any problem.  SaveChanges() does not generate an exception all the time.  It works hundreds of times before generating this error.


    Hong

    Tuesday, January 1, 2013 5:50 PM
  • Because the exception that is being given is, "the code is optimized or a native frame is on top of the call stack", lets make sure that the debug mode was not set to optimized the code on compile. On the main menu click on the Debug -> Mvc... Properties. When the Property page open click on the Build tab along the left side of the page. Then make sure that Optimize code check box is NOT check. If it is checked then remove the check mark re-compile and run in that configuration if it is already NOT checked then I would suggest that you place a breakpoint in the catch block Exception where you know it will be caught the you should be able to look at the exception and inner exception.

      

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 1, 2013 6:24 PM
  • Fernando, "Optimize" code is unchecked.  I did put a break point in the catch block, and that is where I got the following when checking the content of the Exception:

    System.InvalidOperationException] = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

    This is really weird. I have repeated it about half a dozen times, and always got the same result.


    Hong

    Tuesday, January 1, 2013 7:02 PM
  • Hong, then I am at a lost because you are breaking where you get the exception and the exception is stating that the code is ether optimized or a native frame ison top of the call stack. We know it is not optimized and we are in your code so it is not a native frame that is on top of the call stack. About the only thing I can think of doing is to monitor it from the SQL server side to see what SQL server is sending back through something like SQL Server Management Studio. Other then that I don't know what else to tell you to try.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, January 1, 2013 8:31 PM
  • Fernando, I am deeply puzzled too.  Thank you for your suggestion.  I will keep trying.  I will report back if I find anything useful.

    The output console shows the following when that happens:

      A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.Data.Entity.dll

    It is a background thread (I use (new Thread()).Start()).  I do not know if this has anything to do with it.  I will change it to foreground just for debugging. 


    Hong

    Tuesday, January 1, 2013 9:34 PM
  • Hallelujah! The background thread is the issue.  There was no exception when I brought the function to a foreground thread.

    IIS kills background threads after Idle Time-out. Apparently, the debugger of VS cannot handle this situation.  I increased the Idle Time-out of the Application Pool used by the web app, and everything is fine now.

    It would be nice if a caught exception could tell what is going on

     

    Hong

    • Proposed as answer by Alexander Sun Thursday, January 3, 2013 7:26 AM
    • Marked as answer by Hong (MA, USA) Thursday, January 3, 2013 11:13 AM
    Tuesday, January 1, 2013 11:07 PM
  •   

    Nice job on finding the issue.

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, January 2, 2013 4:05 AM