I'm trying to create a generic method to get data from SQL Server by one stored procedure, but it doesn't return any data:
public static IEnumerable<T> Select<T>(string SQL)
{
string spName = "exec spGetData @SQL";
var parametros = new object[]{
new SqlParameter("@SQL", SQL)
};
IEnumerable<T> result;
using (DBContext db = new DBContext())
{
result = from a in db.ObjectContext.ExecuteStoreQuery<T>(spName, parametros).ToList() select a;
}
return result;
}
Someone could help me?
EF handles stored procedures and creates entity result sets from the function mapping of the stored proc result select to be consumed off of the EF context.
Steps
Model Browser
which can be displayed by right clicking the EF design surface and selectingModel Browser
.Model Browser
look for the stored proc in theModel Store
->Stored Procedures / Functions
folder. Verify that the mapping is good by looking at theModel
->Function Imports
and that the result entity can be found in theComplex Types
folder.From there you can call your stored proc off of an EF context and it returns a list of the result set to the code.
I give a more thorough explanation on my blog article and provide hints and work arounds to some problems with complex entity failures : Entity Framework Stored Procedure Instructions