如何从 Asp.net 5 中的 DbUpdateException 中检索异常编号
How do you retrieve the Exception number from DbUpdateException in Asp.net 5
我不想进行字符串比较,看起来我的例外编号在 InnerExeption 编号字段中,但如果我尝试直接访问它,我会遇到构建错误。
try {
db.SaveChanges();
} catch (Microsoft.Data.Entity.DbUpdateException ex) {
// ex.InternalException.Number won't compile but appears to be there
// ex.InternalException.Message does compile but I think it seems less clean than a const comparison
}
一旦我得到了数字,是否有一个常量可以与之比较?我正在尝试检查重复键错误。
谢谢
我找到了方法。
db.User.Add(user);
try {
db.SaveChanges();
} catch (Microsoft.Data.Entity.DbUpdateException ex) {
if (((System.Data.SqlClient.SqlException)ex.InnerException).Number == 2627) {
return new HttpStatusCodeResult(409);
}
}
我不想进行字符串比较,看起来我的例外编号在 InnerExeption 编号字段中,但如果我尝试直接访问它,我会遇到构建错误。
try {
db.SaveChanges();
} catch (Microsoft.Data.Entity.DbUpdateException ex) {
// ex.InternalException.Number won't compile but appears to be there
// ex.InternalException.Message does compile but I think it seems less clean than a const comparison
}
一旦我得到了数字,是否有一个常量可以与之比较?我正在尝试检查重复键错误。
谢谢
我找到了方法。
db.User.Add(user);
try {
db.SaveChanges();
} catch (Microsoft.Data.Entity.DbUpdateException ex) {
if (((System.Data.SqlClient.SqlException)ex.InnerException).Number == 2627) {
return new HttpStatusCodeResult(409);
}
}