使用DbContext
我连接数据库并执行简单查询:
var results = dbContext.Database.SqlQuery<ResultDto>(MyQuery).ToList();
对我来说有点奇怪..我在上面一行之后得到了所有这些例外:
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.SqlServer.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.SqlServer.dll
'MatchingService.Server.Host.ConsoleApp.exe' (CLR v4.0.30319: MatchingService.Server.Host.ConsoleApp.exe): Loaded 'EntityFrameworkDynamicProxies-EntityFramework'.
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.dll
Exception thrown: 'System.Data.Entity.Core.EntityCommandExecutionException' in EntityFramework.SqlServer.dll
我的问题是:为什么我在Visual Studio输出窗口中获取它们...但我无法在try catch中捕获它们。这是我做的(没有成功):
try
{
var results = dbContext.Database.SqlQuery<ResultDto>(MyQuery).ToList();
}
catch (System.Data.SqlClient.SqlException sqlException)
{
}
catch (Exception ex)
{
}
最重要的部分(我完全忘了) 我收到了很好的数据..这条线基本上有效!但是将异常抛出到Output窗口......