实体框架4.2:正确的数据库错误 [英] Entity Framework 4.2 : Getting proper database errors
问题描述
我尝试插入相同的数据,以查看我收到的内容,但是我收到以下错误:
更新条目时出错。查看内部异常
的详细信息。
内部异常我可以看到关于唯一约束的完整错误。但是,如何唯一地捕获这个异常来告诉用户:
你再次输入相同的值。
这是我目前所做的:
try
{
UpdateModel< ConditionType>(conditionType,null,null,new string [] {ConditionTypeId});
_conditionTypeRepository.Save();
return RedirectToAction(conditiontype);
}
catch(Exception ex)
{
ModelState.AddModelError(,更新时出现错误:+ ex.Message);
}
但这是一种通用的方法。我想做的是提供一个特定的信息。
任何想法?
编辑:
我累了以下,但这次没有抓住它:
catch(SqlException ex)
{
if(ex.Number == 2627)
{
ModelState.AddModelError(,你是再次输入相同的值。);
}
ModelState.AddModelError(,更新值时出现错误:+ ex.Message);
}
我挖了一点,结果是它抛出一个异常类型的 System.Data.Entity.Infrastructure.DbUpdateException
不包含异常号码。
编辑:
这里我如何解决问题,但我相信这不是解决问题的最好方式。任何想法如何重构这个代码?
catch(Exception ex){
if(ex。 InnerException.InnerException.GetType()== typeof(SqlException)){
if(((SqlException)ex.InnerException.InnerException).Number == 2627)
ModelState.AddModelError( ,你再次输入相同的值);
else
ModelState.AddModelError(,更新值时出现错误:+ ex.Message);
} else {
ModelState.AddModelError(,更新值时出现错误:+ ex.Message);
}
}
你可以这样做来寻找一个SqlException的内部异常,然后以不同的方式处理sql异常。
catch(异常ex)
{
异常当前= ex;
SqlException se = null;
do
{
se = current.InnerException as SqlException;
current = current.InnerException;
}
while(current!= null&& se == null);
if(se!= null)
{
//在这里执行SqlException处理
}
else
{
//在此处执行其他异常处理
}
}
In my ASP.NET MVC 3 application, I use EF 4.2. In my database, I have a unique constraint for a column.
I try to insert same data in order to see what I get but I am getting the below error:
An error occurred while updating the entries. See the inner exception for details.
Inside the inner exception I can see the full error about unique constraint. But how can I uniquely catch this exception to tell the user this:
You are entering the same value again.
Here is what I do currently:
try
{
UpdateModel<ConditionType>(conditionType, null, null, new string[] { "ConditionTypeId" });
_conditionTypeRepository.Save();
return RedirectToAction("conditiontype");
}
catch (Exception ex)
{
ModelState.AddModelError("", "There was an error while updating: " + ex.Message);
}
But this is a generic approach. What I would like to do is to provide a specific message.
Any thoughts?
Edit:
I tired the below but this time it didn't catch it:
catch (SqlException ex)
{
if (ex.Number == 2627)
{
ModelState.AddModelError("", "You are entering the same value again.");
}
ModelState.AddModelError("", "There was an error while updating the value: " + ex.Message);
}
I dug into a little bit and it turned out that it throws an exception type of System.Data.Entity.Infrastructure.DbUpdateException
which does not contain the exception number.
EDIT:
Here how I solve the problem but I am sure it is not the best way of solving it. Any idea how to refactor this code?
catch (Exception ex) {
if (ex.InnerException.InnerException.GetType() == typeof(SqlException)) {
if (((SqlException)ex.InnerException.InnerException).Number == 2627)
ModelState.AddModelError("", "You are entering the same value again.");
else
ModelState.AddModelError("", "There was an error while updating the value: " + ex.Message);
} else {
ModelState.AddModelError("", "There was an error while updating the value: " + ex.Message);
}
}
You could do something like this to look for an inner exception that is a SqlException and then handle the sql exception differently.
catch(Exception ex)
{
Exception current = ex;
SqlException se = null;
do
{
se = current.InnerException as SqlException;
current = current.InnerException;
}
while (current != null && se == null);
if (se != null)
{
// Do your SqlException processing here
}
else
{
// Do other exception processing here
}
}
这篇关于实体框架4.2:正确的数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!