我的异常处理分散在各种形式中。由于我需要详细的异常处理,因此代码有点复杂,如:
catch (Exception ex)
{
var conn = ((IObjectContextAdapter)this).ObjectContext.Connection as
EntityConnection;
if (conn.ConnectionString.IndexOf(Providers.SqlCe4) >= 0)
{
#region deal with exceptions
Exception e = ex;
SqlCeException sce = null;
while (e != null)
{
sce = e.InnerException as SqlCeException;
if (sce != null) break;
e = e.InnerException;
}
if (sce != null)
{
SqlCeError = sce;
switch (sce.NativeError)
{
case 25016:
SaveErrorMessage = Localization.Texts.SqlCeException25016;
break;
...
而不是复制&每次查询或保存更改时粘贴,如何在一个地方集中异常处理?是创建DbContext / ObjectContext的部分类,并在那里进行所有查询和保存更改以及处理异常的好方法吗?