简体   繁体   English

从存储过程获取数据到EF6 MVC模型

[英]Get Data from Stored Procedure to Model EF6 MVC

I have been reading how to use stored procedures in EF6 我一直在阅读如何在EF6中使用存储过程

https://www.entityframeworktutorial.net/entityframework6/code-first-insert-update-delete-stored-procedure-mapping.aspx 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 我尝试使用Fluent API,但没有选择选项

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

I'm using Code First approach. 我正在使用Code First方法。

I decided to use Dapper when mapping SP's to models 在将SP映射到模型时,我决定使用Dapper

public async Task OnGetListAsync() { IDbConnection con = new SqlConnection(_config.GetConnectionString("DefaultConnection")); 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());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM