Home > Blockchain >  EF .FromSqlRaw call ending "500, An unhandled error occurred"
EF .FromSqlRaw call ending "500, An unhandled error occurred"

Time:07-16

.netcore 3.1, EF 5.0, C#:

[HttpGet]
public IActionResult Get()
{
  var r = _context.mymodel.FromSqlRaw<mytype>("[dbo].[test]");
  return Ok(r);
}

Breakpoint at return Ok(r) shows no Exception, but always

{
  "statusCode": 500,
  "message": "An unhandled error occurred"
}

Adding try...catch..., it never steps into catch

public IActionResult Get()
{
  try {
    var r = ...
    return Ok(r);
  }
  catch (Exception e) {
    return StatusCode(500, e.Message);  // <-- never reached.
  }
}

Also tried public async Task<IActionResult> Get() got same error.

How to identify the unhandled exception?

CodePudding user response:

I did some simple tests according to the enter image description here enter image description here

What is your database structure and model like? Does [dbo].[test] match the fields in _context.mymodel?

CodePudding user response:

Two issues found and resolved:

  1. A parameter is guid type, when passing in as a string, DebugView->Query, ran ok at SQL, but EF API fails no exception.
  2. Two columns returned from SP sharing the same name in diff case, like [orderid] and [ORDERID] (from diff tables). SQL runs ok, not for EF, again no exception.
  • Related