1

我正在使用存储过程并使用Microsoft Entity Framework Core提供的“FromSql”功能运行存储过程。实体框架不捕获SQL异常

当出现SQL异常时,Entity Framework根本没有发现异常。

例如下面的例子,SQL中缺少存储过程“GetAppSections”。当我运行应用程序时在调试模式下,我可以在本地窗口深处找到“missing stored proc”错误。

但是逻辑永远不会进入“Catch”块。实体框架只是运行'FromSql'命令并返回一个空对象。 “catch”块永远不会被击中。

任何想法为什么实体框架没有捕获SQL中的“缺少存储过程”异常?

public virtual IEnumerable<appSection> GetSections() 
    { 
     try 
     { 

      return _context.appSections.FromSql("dbo.GetAppSections @p0", 
            parameters: new[] { _standarParams.userID.ToString()}).AsEnumerable(); 

     } 
     catch (Exception ex) 
     { 
      // error handling code 
     } 
    } 
+0

难道是被抑制的回报?也许尝试使用var来捕获结果。 –

+0

谢谢。发布前我曾尝试过。正如Steve建议的那样将IEnumerable更改为ToList。 – mvc234

回答

1

您正在返回推迟的IEnumerable。您需要围绕实际访问IEnumerable(ToList,ForEach等)的代码尝试catch代码块。

herehere

+0

非常感谢!我将AsEnumerable()更改为ToList(),现在我能够捕捉到异常! – mvc234