I have created a new ASP.NET Core 2.1 web application with Angular
Visual Studio creates a project that uses the Entity Framework Core with ASP.NET Core MVC.
I have this problem: I have to read records from a stored procedure:
CREATE PROCEDURE GetEmployees
(@PageIndex INT,
@PageSize INT)
AS
BEGIN
SELECT *
FROM employee
ORDER BY id
OFFSET @PageSize * (@PageIndex - 1) ROWS
FETCH NEXT @PageSize ROWS ONLY;
SELECT COUNT(*) AS totalCount
FROM employee;
END
I found a question that almost solves my question, but unfortunately there is still something that does not work.
This is my code:
namespace Angular.Controllers
{
//[Produces("application/json")]
[Route("api/Employees")]
public class EmployeesController : Controller
{
private readonly ApplicationDbContext _context;
public EmployeesController(ApplicationDbContext context)
{
_context = context;
}
// GET: api/Employees/pageIndex/1/pageSize/1
[HttpGet("pageIndex/{pageIndex}/pageSize/{pageSize}")]
public Task<IActionResult> GetEmployeeP([FromRoute] int pageIndex, int pageSize)
{
SqlParameter pageIndexParam = new SqlParameter("@PageIndex", SqlDbType.Int);
pageIndexParam.Value = pageIndex;
SqlParameter pageSizeParam = new SqlParameter("@pageSize", SqlDbType.Int);
pageSizeParam.Value = pageSize;
// SqlParameter pageIndexParam = new SqlParameter("@PageIndex", pageIndex);
// SqlParameter pageSizeParam = new SqlParameter("@pageSize", pageSize);
var cmd = _context.Database.GetDbConnection().CreateCommand();
cmd.CommandText = "GetEmployees"; // The name of the stored procedure
cmd.CommandType = System.Data.CommandType.StoredProcedure;
// the 2 parameters to be passed to the procedure
var param = cmd.CreateParameter();
param.ParameterName = "@PageIndex";
param.Value = pageIndexParam;
cmd.Parameters.Add(param);
var param2 = cmd.CreateParameter();
param2.ParameterName = "@pageSize";
param2.Value = pageSizeParam;
cmd.Parameters.Add(param2);
try
{
// connection.Open();
// _context.Database.GetDbConnection().Open(); // it crashes after this line
_context.Database.OpenConnection(); // it crashes after this line
var dr = cmd.ExecuteReader(); // ArgumentException: No mapping exists from object type System.Data.SqlClient.SqlParameter to a known managed provider native type.
List<Employee> listEmp = new List<Employee>();
while (dr.Read())
{
// Is there a faster way to read the whole record?
// Or should I write a line for each field?
Employee emp = new Employee();
emp.ID = System.Int32.Parse(dr["id"].ToString());
emp.Fname = dr["FName"].ToString();
emp.email = dr["email"].ToString();
emp.Lname = dr["LName"].ToString();
listEmp.Add(emp);
dr.NextResult();
}
return Ok(listEmp);
}
catch (Exception ex)
{
// how can I return an error in json format?
throw;
}
}
}
}
The problem is in the line where the script ExecuteReader:
ArgumentException: No mapping exists from object type System.Data.SqlClient.SqlParameter to a known managed provider native type.
I use Microsoft SQL Server In the Startup.cs file, I configured the connection in this way:
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("Server=localhost\\SQLEXPRESS;Database=master;Trusted_Connection=True;"));
Can you help me?
Maybe I managed to pass the parameters correctly (see my solution, in the answer) but I can not extract the records