3

EDIT 3

To be clear, at this point:

1) I am seeing SQL Exceptions in the output but it is not "Catching" any exceptions... code is executing normally and the only reason I know there are exceptions is that I am looking at the output window. Entity seems to work for the initial read (it returned expected data).

2) Entity hangs when I try to so a .SaveChanges but I cannot seem to "Catch" any exceptions... it just hangs. No idea why?

Original Post I have a C# console application, using .Net 4.5 using Entity Framework 6.1.3

I am able to read my database so I know the connection string and credentials are correct. My issue is that when I do a DbContext.SaveChanges() my application hangs and if I hang my cursor over the SaveChanges method in Visual Studio, the pop-up shows that it has thrown :

System.Data.Entity.InfrastructureDbUpdateException
System.Data.Entity.InfrastructureConcurrencyException
System.Data.Entity.Validation.DbEntityValidationException
System.NoSupportedException
System.ObjectDisposedException
System.InvalidOperationException

I've tried to capture a few of these as per the code below but in reading other SO articles, it seems that I cannot catch these exceptions? Does anyone know HOW to catch these?

Please note that prior to doing these SaveChanges, I was able to read from another table so I know my credentials and connections string work and that I have connectivity and entity seems to at least be working.

One more piece of information: this used to work a few days ago... I just don't know what I could have done to "break it"! Using SQL Server on another server.

Connection string is:

<add name="DataContext" 
     connectionString="data source=1.2.3.4;initial catalog=db1;user id=user1;password=pwd1;MultipleActiveResultSets=True;App=EntityFramework" 
     providerName="System.Data.SqlClient" />

So bottom line is that my application hangs when executing .SaveChanges and I have no idea why...

try
{
    return _context.SaveChanges();
}
catch (DbUpdateException e)
{
    Console.WriteLine(e.ToString());
    Console.ReadLine();
    return 0;
}
catch (DbEntityValidationException dbEx) // added for debug.
{
    foreach (var validationErrors in dbEx.EntityValidationErrors)
    {
        foreach (var validationError in validationErrors.ValidationErrors)
        {
            Trace.TraceInformation("Property: {0} Error: {1}",
                                   validationError.PropertyName,
                                   validationError.ErrorMessage);
            Console.WriteLine(string.Format("Property: {0} Error: {1}",
                                            validationError.PropertyName,
                                            validationError.ErrorMessage));
            Console.ReadLine();
        }
    }

    return 0;
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
    Console.ReadLine();
    return 0;
}

EDIT 1

Exceptions are confirmed to not be real. They are a pop-up provided by either ReSharper or MS Productivity tools to show me possible exceptions... I guess.

So I'm left with just a hang on .SaveChanges with no way to know why?

EDIT 2

I do see that in the first database read, the debug output window shows exceptions even though the read worked! Note that this is also inside a try / catch (Catch (Exception e)) block and the exceptions are NOT getting caught!

A first chance exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.dll
A first chance exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll

Ed Landau
  • 966
  • 2
  • 11
  • 24
  • Check the InnerException message and edit your post to add it. – CodeNotFound Nov 21 '15 at 17:14
  • How? I can't even catch the outer exception. I just know there are exceptions by hovering my mouse over SaveChanges but the application is hung. – Ed Landau Nov 21 '15 at 17:22
  • Then click "View Details" when you hover it – CodeNotFound Nov 21 '15 at 17:23
  • Maybe I'm wrong and the hover pop-up is just showing "possible" exceptions? In that case, the application is just hung and I have no clue why. :(. I've got ReSharper and MS Power Productivity Tools so one of those may be display the list of possible exceptions? – Ed Landau Nov 21 '15 at 17:24
  • Ok. Then remove DbUpdateException `type and put Exception type. Which is the general exception type. And execute again :) – CodeNotFound Nov 21 '15 at 17:26
  • Confirmed. The exception pop-up is there when code it not running :). By the way, Exception type is the latch catch I have.. the catch-all which is what i started with in the first place. Nothing is being caught. – Ed Landau Nov 21 '15 at 17:27
  • Activate throw all exceptions and deactivate the option debug just my code. Maybe that way you can get more infos. – Andre Nov 21 '15 at 17:39
  • Can you replicate the SQL that your .SaveChanges is trying to do? For instance, if you're trying to run an update or delete or something, can you execute the raw sql itself? – Jack Marchetti Nov 21 '15 at 18:01
  • 1) Not sure how to "Activate Throw". 2) Trying to run in Release mode. Getting exceptions for references which were there in Debug... not sure but tacking those down now. 3) 'Not sure how to determine what SQL Entity is generating to replicate them raw. They were translated from SQL in the first place (I started with raw SQL). – Ed Landau Nov 21 '15 at 18:41
  • I think you simply should continue the program by pressing f5. The popup is probably an automatic break because the VS setting "break on exceptions when thrown" is enabled. – Gert Arnold Nov 21 '15 at 21:18
  • Added "Edit 3". Sorry if I was not clear. It is not breaking. It is not "catching". It just hangs! – Ed Landau Nov 21 '15 at 22:21
  • Few things to try: 1) As Andre says, change the debug options for managing exceptions: https://msdn.microsoft.com/en-us/library/x85tt0dd.aspx 2) The fact the app is hanging may mean the exceptions you are seeing in output have already been caught and handled, and the hang is occurring later in the code, possibly for unrelated reasons 3) Download the Entity Framework source and see if you can get more insight 4) Along with having a good decompiler installed you can use the Pause button in Visual Studio to find out what method is executing while your app is hanging – cbp Nov 21 '15 at 22:34
  • Thinking about the problem itself: You say the code worked previously, but not now. This sounds like it is dependent on the data that is being loaded/saved. The issue is probably due to a misconfiguration in your entity mappings. Without seeing your code and mappings I have no idea what, but that's where I would start. – cbp Nov 21 '15 at 22:36

1 Answers1

0

You're problem is in the e.ToString(). You want to check the e.Message property. You can also check to see if there is an e.InnerException as well.

catch (Exception e)
{
    if (e.InnerException != null) {
       Console.WriteLine(e.InnerException);
    } else {
       Console.WriteLine(e.Message);
    }

    Console.ReadLine();
    return 0;

}
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117