我宁愿不进行字符串比较,并且看起来我的exeption号在InnerExeption number字段中,但是如果我尝试直接访问它,则会出现构建错误。
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
}
一旦获得数字,便可以将其与const进行比较吗?我正在尝试检查重复的键错误。
谢谢
我找到了一种方法。
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句