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