ASP.NET Core and EF Core 1.1 - Diplay Data using Stored Procedure

asp.net-core entity-framework-core

Question

I have problem with my query. I want to display the result on a view.

[HttpGet]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Index()
{
    return View(await _Context.Employee
                              .FromSql("EXEC sp_GetLoanDetails")
                              .ToArrayAsync());
}

Here is my list of items I want to view:

public class StoredProcRow
{
    [Key]
    public int empID { get; set; }
    public string empFullName { get; set; }
    public double EducationalLoan { get; set; }
    public double PettyCash { get; set; }
    public double BusinessLoan { get; set; }
    public double ApplianceLoan { get; set; }
    public double EmergencyLoan { get; set; }
    public double AllPurposeLoan { get; set; }
    public double KAPUSOIILoan { get; set; }
    public double FiestaLoan { get; set; }
    public double SalaryLoan { get; set; }
    public double Pledge { get; set; }
    public double PagIbigLoan { get; set; }
    public double SSSLoan { get; set; }
    public double AllAroundLoan { get; set; }
    public double Total { get; set; }
}

Note: These entities name are the same as entities on column name in sp_GetLoanDetails

Is this achievable right now on EF Core 1.1? Or do I need to go back to manual ADO.NET code?

Thanks!

1
1
12/13/2019 9:41:05 AM

Accepted Answer

While support for stored procedures isn’t completely there with Entity Framework Core yet, you still can use FromSql to consume stored procedures with it.

In order to do that, the database context needs to know the entity you are mapping to from the stored procedure. Unfortunately, the only way to do that right now is to actually define it as an entity in the database context:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<StoredProcRow>(entity =>
    {
        // …
    });
}

Then, you can consume the stored procedure by running the FromSql method on a set for that entity:

public virtual IQueryable<StoredProcRow> GetLoanDetails()
{
    return Set<StoredProcRow>().FromSql("[sp_GetLoanDetails]").AsNoTracking();
}

Note that I’m using a AsNoTracking here to avoid the data context to track changes to entities that come from the stored procedure (since you don’t have a way to update them anyway). Also I’m using Set<T>() inside the method to avoid having to expose the type as a member on the database context since you cannot use the set without the stored procedure anyway.

Btw. you don’t need (not sure if that even works) EXEC in the sql statement you pass to FromSql. Just pass the stored procedure name and any arguments to it, e.g.:

Set<MyEntity>().FromSql("[SomeStoredProcedure]");
Set<MyEntity>().FromSql("[SProcWithOneArgument] @Arg = {0}");
Set<MyEntity>().FromSql("[SProcWithTwoArguments] @Arg1 = {0}, Arg2 = {1}");
8
3/29/2017 8:24:55 AM

Popular Answer

From NuGet add System.Data.Common and System.Data.SqlClient. These allow ADO.NET commands to be run i.e. a stored procedure.

https://forums.asp.net/post/6061777.aspx



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow