Entity Framework 没有捕捉到 SQL 异常

Entity Framework does not catch SQL Exception

我正在使用存储过程,并且 运行使用 Microsoft Entity framework Core 提供的 "FromSql" 功能来连接存储过程。

当出现SQL异常时Entity Framework根本不捕获异常

例如,SQL.When 中缺少存储过程 "GetAppSections" 我 运行 应用程序 在调试模式下,我可以找到 "missing stored proc" 错误的深处本地 window.

然而,逻辑永远不会进入 "Catch" block.Entity 框架,只是 运行s 'FromSql' 命令和 returns 一个空对象。 "catch" 块永远不会被击中。

知道为什么 Entity Framework 没有捕获来自 SQL 的 "missing stored proc" 异常吗?

 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
        }
    }

您返回的 IEnumerable 已延迟。您需要在实际访问 IEnumerable(ToList、ForEach 等)的代码周围使用 try catch 块。

here and