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