【发布时间】:2020-03-19 00:49:33
【问题描述】:
我试图在 Entity Framework Core 3.0 中调用过程时捕获错误,但它从未遇到 catch 块。我尝试强制执行一些常见错误,例如错误的过程名称或错误的参数数量。
我实际上可以单步执行代码,这不会出错,并且我可以在结果视图中看到这些消息。
Invalid object name... An insufficient number of arguments were supplied for the procedure or function...
我在这里想念什么?我怎样才能捕捉到这样的错误?
try
{
IEnumerable<Users> result = (from x in _db.Users.FromSqlRaw("Execute ustp_MyProcedure)").AsEnumerable()
select x);
return result;
}
catch (Exception ex)
{
//Never gets here
}
更新
我仍然很困惑为什么 EntityFramework Core 3.0 的行为与我在旧 MVC 应用程序中使用的 EF6 不同。我仍然在这里使用 IEnumerable,但是这个设置中的错误最终会出现在 catch 块中。
private DatabseEntities db = new DatabseEntities();
public IEnumerable<ustp_MyProcedure_Result> MyMethod(string searchString)
{
try
{
var result= (from p in db.ustp_MyProcedure(searchString)
select p);
return sourceQuery;
}
catch (Exception ex)
{
//Even as an IEnumerable, errors DO end up in this catch block
}
}
【问题讨论】:
标签: c# asp.net-core error-handling entity-framework-core