I need to show a list in a DataGridView but due to the complexity of the SQL query I prefer not to use Lin-Q so I create a stored procedure but since I use the Entity Framework I do not want to call using SQLConnection but from DbContext.
I need to show a list in a DataGridView but due to the complexity of the SQL query I prefer not to use Lin-Q so I create a stored procedure but since I use the Entity Framework I do not want to call using SQLConnection but from DbContext.
To use stored procedures call the method Database.SqlQuery
here you pass a class TElement
with the names of the columns that the stored procedure returns and then pass the parameters an example:
public List<Persona> RetornarListado(string nombres, string apellidos) {
using(var db = new NameContext()) {
var listado = db.Database.SqlQuery<Persona>(
"SPObtenerPersonas @filtronombres, @filtroapellidos",
new SqlParameter("@filtronombres", nombres),
new SqlParameter("@filtroapellidos", apellidos));//aquí usas el método
return listado.ToList();
}
}