Entity framework 4 如何在Entity Framework 4中处理重复密钥异常
在将记录更新为数据库中已经存在的记录时,是否有人有一种简单的方法来处理此异常?尝试以下方法:Entity framework 4 如何在Entity Framework 4中处理重复密钥异常,entity-framework-4,Entity Framework 4,在将记录更新为数据库中已经存在的记录时,是否有人有一种简单的方法来处理此异常?尝试以下方法: catch (UpdateException ex) { SqlException innerException = ex.InnerException as SqlException; if (innerException != null && innerException.Number == ??????) { //Place you
catch (UpdateException ex)
{
SqlException innerException = ex.InnerException as SqlException;
if (innerException != null && innerException.Number == ??????)
{
//Place you exception code handling here..
}
else
{
throw; //(bubble up)
}
}
这是一个简单的解决方案,但如果错误号不太可能发生变化,您将来可能会遇到问题。尝试以下方法:
catch (UpdateException ex)
{
SqlException innerException = ex.InnerException as SqlException;
if (innerException != null && innerException.Number == ??????)
{
//Place you exception code handling here..
}
else
{
throw; //(bubble up)
}
}
这是一个简单的解决方案,但如果错误号不太可能发生变化,您将来可能会遇到问题。如果要(减少到问号的数量),请务必引用它。如果要(减少到问号的数量),请务必引用它。可能重复的可能重复的