Razor - Best way to get result from Stored Procedure

Brian 161 Reputation points
2021-10-08T21:09:00.917+00:00

I have a complex stored procedure that always returns basic data (integers) in 1 row and 20 columns. I need to simply call the stored procedure and display the result on my razor page.

I'm able to do this using EF by creating a poco class for the result.

sing Microsoft.EntityFrameworkCore;

namespace myApp.Entities
{
[Keyless]
public class Result
{
public int Col1 { get; set; }
public int Col2 { get; set; }
....
public int Col20 { get; set; }
}

Then using FromSqlRaw in my PageModel:

Result= _db.MyEntity.FromSqlRaw($"exec MyStoredProcedure '{param}'").AsEnumerable().FirstOrDefault();

Then on my razor page, I can retrieve all the values by:

@默 .Result.Co11
@默 .Result.Col2
....
@默 .Result.Col20
etc

This works well, however having to create the Result class and list 20 columns for this one page feels excessive. I'm wondering if there's a better way to retrieve the values from the stored procedure without using EF.

Any recommendations are greatly appreciated.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,157 questions
{count} votes