如何处理实体框架的存储过程异常消息

时间:2017-06-15 21:13:10

标签: entity-framework sql-server-2008 stored-procedures sql-server-2012 throw

如何从sql存储过程到实体框架捕获RAISEERROR或SQL 2012 - THROW异常 - 存储过程调用?

using (Entities context = new Entities())
{
        IEnumerable<EmployeeDetails> empDetails  =  context. Database.SqlQuery
                                                                      < EmployeeDetails >("exec GetEmployeeData ", null).ToList();
}

1 个答案:

答案 0 :(得分:0)

尝试/抓住?

using (Entities context = new Entities())
{
    try
    {
        IEnumerable<EmployeeDetails> empDetails  =  context. Database.SqlQuery                                                            <EmployeeDetails>("exec GetEmployeeData ", null).ToList();
    }
    Catch (SqlException ex)
    {
        //Do something
    }
}