0

我一直在阅读如何在 EF6 中使用存储过程

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

但它并没有显示我如何实际从过程中获取数据到模型。

例如我有这个查询

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

我想用它填充我的模型,属性是从过程返回的表

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

我尝试使用 Fluent API,但没有选择选项

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

我正在使用代码优先方法。

4

1 回答 1

0

我决定在将 SP 映射到模型时使用 Dapper

公共异步任务 OnGetListAsync() { IDbConnection con = new SqlConnection(_config.GetConnectionString("DefaultConnection"));

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

    return new JsonResult(query.ToList());
}
于 2019-05-28T13:36:43.693 回答