【发布时间】:2026-01-05 10:50:01
【问题描述】:
我将SaveChanges() 方法放在try/catch 块中,但我无法捕获SqlExeption。
try
{
db.SaveChanges();
}
catch (Exception ex)
{
}
【问题讨论】:
-
所需的行为已解释清楚且显而易见。有明确的问题陈述。好奇为什么这被否决...
-
我打电话给 BS 来结束这个话题。它显然是主题,甚至有示例代码显示预期的行为是什么。 '希望我们可以否决那些投票关闭它的人。
-
应该重新打开并分配接受的答案@usr
-
这个问题很好,只是措辞不好。它应该是这样说的:
I am trying to catch SqlException to handle database errors thrown by SaveChanges(). However the exception is never caught when I use 'catch (SqlException ex)'. If instead I put 'catch(Exception ex)' then it does work. Why is this? The type of exception in the debugger shown as SqlException.
标签: c# entity-framework try-catch