question

Brian-7574 avatar image
0 Votes"
Brian-7574 asked Brian-7574 commented

Razor - Best way to get result from Stored Procedure

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:

@Model.Result.Co11
@Model.Result.Col2
....
@Model.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.





dotnet-entity-framework-coredotnet-aspnet-core-razor
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I'm wondering if there's a better way to retrieve the values from the stored procedure without using EF.

I don't think so. ADO.NET is an option but then you'll need to even write more code to populate the model from a reader. The benefit of EF is it populates the model.







0 Votes 0 ·

The point of an ORM like EF or even dapper, is the typed response for each query. As c# is statically typed, you need to define the type.

If you do not want to use types, then just use the underlying ado.net library. It pretty simple.

0 Votes 0 ·

Thanks for the replies. That makes sense.

Since I'll be doing multiple times throughout my application, I'll probably make a Sprocs folder inside my Models folder and put all of my stored procedure classes in there.

0 Votes 0 ·

0 Answers