The*_*Man 7 c# entity-framework asp.net-mvc-3
我在MVC3中创建了一个REST API,我需要做的就是在更新或创建操作失败时返回描述性消息.
消息"The update operation failed while creating the customer object."不够好.
我的下一个想法是从异常中获取消息,返回如下内容:
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Business_Category". The conflict occurred in database "MyDb_Dev", table "dbo.Category", column 'CategoryID'.
The statement has been terminated.
Run Code Online (Sandbox Code Playgroud)
这似乎是太多的信息.我可以解析出column "CategoryID"异常消息......不确定这是否可靠.
是否可以为字段设置属性,以便在失败时我可以获得该字段的错误描述?我愿意接受建议.
小智 0
听起来您需要使用 Exception.Data。更多信息可以在这里找到:
http://msdn.microsoft.com/en-us/library/system.exception.data.aspx