How to call a Store Procedure that returns a list of data in the Entity Framework Code First?

1

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.

    
asked by 09.01.2017 в 02:12
source

1 answer

1

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();
      }
 }
    
answered by 09.01.2017 / 02:17
source