首页 > 解决方案 > 动态检索存储过程的结果

问题描述

我有一个如下所示的休息端点。现在基于 ReportSource 名称,我想执行该存储过程。有 40 个这样的存储过程,我不想为每个存储过程创建实体类。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BAService.Dtos
{
    class BARequestDTO
    {
        public int ReportID { get; set; }
        public string ReportName { get; set; }
        public DateTime? AsOfDate { get; set; }
        public string ReportSource { get; set; }
    }
}

这是控制器:

[HttpPost]
[Route("BAReportRequest")]
public string BAReportRequest(BARequestDTO dto)
{
    try
    {
        var test = _bIReportRepositoryBA.RunReport(dto);
    }
    catch (Exception ex)
    {
        _LogService.Error(string.Format("Unable to save data dictionary"), ex);

    }
    return test;
}

我知道执行存储过程的唯一方法是像下面这样调用它。在这种方法中,我将不得不创建 40 个可以从数据库接收结果的新实体对象。我很懒,我正在寻找一种执行存储过程并在 json 中获取结果而不为每个存储过程创建实体对象的通用方法。

public List<EmployerDashboardDetailsDto> RunReport(BIReportDto dto)
{
    return _DBContext.Database
                    .SqlQuery<usp_EmployerDashboardStatistics>("report.usp_Report1 @exchangeID", new SqlParameter("@exchangeID", dto.ID))
                    .ToList();
}

标签: c#sqlentity-framework

解决方案


推荐阅读