Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从实体框架中提取用户友好的错误消息_C#_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

C# 从实体框架中提取用户友好的错误消息

C# 从实体框架中提取用户友好的错误消息,c#,entity-framework,asp.net-mvc-4,C#,Entity Framework,Asp.net Mvc 4,我的asp.net mvc web应用程序中包含以下代码:- try { var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1)); repository.Sa

我的asp.net mvc web应用程序中包含以下代码:-

try
            {
                var count = repository.changeDeviceSwitch(s.Switch.SwitchID, (Int32)s.GeneralSwitchTo, User.Identity.Name.Substring(User.Identity.Name.IndexOf("\\") + 1));
                repository.Save();
                return Json(new { IsSuccess = "redirect", description = Url.Action("Details", new { id = s.GeneralSwitchTo }) }, JsonRequestBehavior.AllowGet);

            }
            catch (DbUpdateException exception)
            {
                return Json(new { IsSuccess = "custome", description = "Error occurred." + exception.InnerException.InnerException.Message.ToString() }, JsonRequestBehavior.AllowGet);

            }
            catch (Exception e)
            {
                return Json(new { IsSuccess = "custome", description = "Error occurred." }, JsonRequestBehavior.AllowGet);
            }
它将调用以下存储库方法:-

 public int changeDeviceSwitch(int fromID , int toID, string username)
        {

            var currentdevices = tms.TMSSwitchPorts.Where(a => a.SwitchID == fromID);
            int count = 0;
            foreach (var d in currentdevices)
            {

                DeletePort(d, username);
                //d.SwitchID = toID;

                count++;

            }
            foreach (var d in currentdevices)
            {

                TMSSwitchPort tsp = new TMSSwitchPort() { SwitchID = toID, TechnologyID = d.TechnologyID, PortNumber = d.PortNumber };
                InsertOrUpdatePort(tsp, username);

            }

            return count;

        }
当前,如果发生DbUpdateException,用户将获得以下信息:-

违反主键约束“PK_SwitchPortServer”。不能 在对象“dbo.TMSSwitchPorts”中插入重复键。复制钥匙 数值为(1484,e)。声明已终止


当用户尝试添加同一记录下已存在的端口号时,将发生此异常。(SwitchID+Portno)组合在数据库中是唯一的。这纯粹是技术信息,所以我不确定是否有一种方法可以从entity framework中提取出更用户友好的错误信息?或者我需要自己做检查?

抛出自己的异常怎么样

try
{
    InsertOrUpdatePort(tsp, username);
}
catch (DbUpdateException e)
{
    throw new Exception("Friendly message here", e)
}

更好的方法是在这里定义您自己的异常类,而不仅仅是
exception

,但是DBupdateException在某种程度上是通用的,可能会由于不同的原因而引发,因此编写我自己的错误消息可能因其他原因无效,您认为呢?@johnG,您是对的。但是,无法更改从DB抛出的内容。实际上我只是想到了另一种逻辑。如果你期望这样的事情发生,那么这不是一个例外,这意味着你应该自己做检查。但是,您可以在上面的
catch
中进行检查-这将确保您仅在必要时进行检查,如果唯一性检查失败,我应该返回什么类型的消息?比方说,我将检查我的存储库方法中是否已经存在Portno+SwitchID,我将如何将此错误传递给我的操作方法?@johnG,当涉及到层间消息传递时,有各种各样的设计。在一个项目中,DB层不返回对象本身,通过自定义<代码>操作结果< /代码>字段结果、错误等。如果在您的情况下这是过度的,请考虑将此检查暴露为API动作,在调用Update函数之前可以调用。然后一切都可以在行动层面上决定