I have a stored procedure that contains dynamic select. Something like this:
ALTER PROCEDURE [dbo].[usp_GetTestRecords] --@p1 int = 0, --@p2 int = 0 @groupId nvarchar(10) = 0 AS BEGIN SET NOCOUNT ON; DECLARE @query NVARCHAR(max) SET @query = 'SELECT * FROM CUSTOMERS WHERE Id = ' + @groupId /* This actually contains a dynamic pivot select statement */ EXECUTE(@query); END
In SSMS the stored procedure runs fine and shows result set.
In C# using Entity Framework it shows returning an int
instead of IEnumerable
?
private void LoadTestRecords() { TestRecordsDBEntities dataContext = new TestRecordsDBEntities(); string id = ddlGroupId.SelectedValue; List<TestRecord> list = dataContext.usp_GetTestRecords(id); //This part doesn't work returns int GridView1.DataSource = list; }
Generated function for usp_GetTestRecords
public virtual int usp_GetTestRecords(string groupId) { var groupIdParameter = groupId != null ? new ObjectParameter("groupId", groupId) : new ObjectParameter("groupId", typeof(string)); return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("usp_GetTestRecords", groupIdParameter); }
In addition to returning output parameters, a stored procedure can return a result set (that is, a result table associated with a cursor opened in the stored procedure) to the application that issues the CALL statement. The application can then issue fetch requests to read the rows of the result set cursor.
Return Value in SQL Server Stored ProcedureIn default, when we execute a stored procedure in SQL Server, it returns an integer value and this value indicates the execution status of the stored procedure. The 0 value indicates, the procedure is completed successfully and the non-zero values indicate an error.
The RETURN statement is used to unconditionally and immediately terminate an SQL procedure by returning the flow of control to the caller of the stored procedure. It is mandatory that when the RETURN statement is executed that it return an integer value. If the return value is not provided, the default is 0.
In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.
I get this when I have a stored procedure that includes an "exec" call into a temporary table, such as:
insert into #codes (Code, ActionCodes, Description) exec TreatmentCodes_sps 0
It appears that Entity Framework gets confused as to what should be returned by the procedure. The solution I've come across is to add this at the top of the sproc:
SET FMTONLY OFF
After this, all is well.
I got the same problem, and found solution here
It should be what you need now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With