I have a Stored Procedure which executes some dynamic SQL. I want to use this Stored Procedure in entity framework 4, but when I try to create a complex type the procedure returns no columns. Is there any way I can force it to return my values and get the entity framework to receive them? Here is a much-simplified example of what I want to do:
CREATE PROCEDURE sp_calculatesalary(@EmployeeId as int)
begin
declare dynsql as varachar(500)
@dynsql='Select @Salary=Salary,@UserName=Username from employee
where EmployeeId='+cast(@EmployeeId as varchar)+ ''
exec(@dynsql)
select @Salary, @UserName
end
But this does not work. Please help me out. Basically, I want to use a Stored Procedure to execute dynamic SQL and return the values to the entity framework.
add following line at beginning of your SP
try this
Believe me. That is enough.
Or you can simply create a complex type based on your query result , and then use collection of the complex type as your query result.
Perhaps you could consider parameterized SQL, if you must do dynamic queries:
have you tried giving aliases to your last Select:
well, i think this is what you are looking for:
this will generate a public partial class sp_calculatesalary_Result in entity framework based DAL.
Well, if EF cannot recognize what your stored procedure must return then create your complex type ahead of time. You can create a complex type by right clicking any where on the model and and add a complex type. Next when you import your stored procedure, you can choose your complex type from the dropdown.