Get Data from Stored Procedure to Model EF6 MVC

536 Views Asked by At

I have been reading how to use stored procedures in EF6

https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-procedure-mapping.aspx

But it doesn't show how can i actually get data from a procedure to a model.

For example i have this query

CREATE PROCEDURE Abastecimentos_Select 

AS
BEGIN
    SELECT * FROM OpenQuery(MACPAC, 'SELECT RD6001 as Referencia, RD6002 as UAP, RD6030 as QTD_AB_PDP_W01 FROM D805DATPOR.TRP060D WHERE RD6001 not like ''%OP%'' and RD6001 not like ''%PT%'' ')
END

I want to populate my model with it, the properties are the tables returned from the procedure

public class Abastecimentos
{
    public string Referencia { get; set; }
    public string UAP { get; set; }
    public float QTD_AB_PDP_W01 { get; set; }
}

I tried using Fluent API but there is no select option

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Abastecimentos>().MapToStoredProcedures(a => a.//No select)
}

I'm using Code First approach.

1

There are 1 best solutions below

0
Jackal On

I decided to use Dapper when mapping SP's to models

public async Task OnGetListAsync() { IDbConnection con = new SqlConnection(_config.GetConnectionString("DefaultConnection"));

    var query = await con.QueryAsync<ConsumoViewModel>("GetConsumoCalculado", commandType: CommandType.StoredProcedure);

    return new JsonResult(query.ToList());
}