I am working on a ASP.net Core project. In that project, I need to execute an SQL string query and retrieve the result to a custom model. I tried to retrieve the query result using context.Database.SqlQuery<ReportModel>
. But Database.SqlQuery
is not supported in ASP.net Core. Please find my code given below:
public List<ReportModel> GetReportDetails(string customQuery)
{
try
{
List<ReportModel> listReportModel = new List<ReportModel>();
var queryResult = context.Database.SqlQuery<ReportModel>(customQuery);
foreach (var item in queryResult)
{
listReportModel.Add(item);
}
return listReportModel;
}
catch (Exception ex)
{
throw;
}
}
I also tried with the following answer https://stackoverflow.com/questions/35631903/raw-sql-query-without-dbset-entity-framework-core But, using this solution I need to update my context class. I can't update my context class.
Is there any other way to execute the SQL string query in ASP.net Core 3.1? Any help would be appreciable. Thank You!