July 16, 2011
Entity Framework 4 and Cross-Database Stored Procedures Headaches
Posted by Brian Mains under Uncategorized | Tags: .NET, Entity Framework, SQL Server, Stored Procedures |[2] Comments
I figured I’d write a blog detailing the nightmare scenario I encountered in regards to Entity Framework and Stored Procedures in SQL Server, and trying to get Entity Framework to generate the metadata. It all started when creating a complicated search stored procedure, which included subqueries, inner and outer joins, tables variables, etc. For some reason, Entity Framework wouldn’t generate the complex type associated with the stored procedure result set. Everytime I tried to create the function import, I get the error: “The Selected Stored Procedure Returns No Columns”.
Doing some googling found that Entity Framework 4 runs the command SET FMTONLY ON before every stored procedure call. This prevents the stored procedure from actually running against the database, potentially trying to insert data erroneously. But this command, in some if not many/all scenarios, screws up all of us developers in running stored procedure imports. So I went through the cyclical process:
- Script the proc as an alter statement in SQL Server Management Studio (SSMS)
- Add SET FMTONLY OFF in the proc body (after the as keyword)
- Run it
- (I can’t remember if I had to delete/recreate the proc in EF at the moment)
- Ensure the complex type/function import doesn’t exist. Delete if it does.
- Go to EF model, right-click the stored procedure to import, select Add Function Import. Select Create New Complex Type, and it works, no “The Selected Stored Procedure Returns No Columns” message.
- Remove the SET FMTONLY OFF line from the proc, and reexecute the alter statement in SSMS.
create procedure dbo.CustomersSelectAllasdeclare @table table(— fields)insert into @tableselect — long complex queryselect * from @table
create procedure dbo.CustomersSelectAllasset FMTONLY offselect — long complex query
October 19th, 2011 at 4:02 am
hi,
if i use set FMTONLY off in the beginning of the store procedures and return the temporary table data
from stored procedure. does complex types shows the columns?
November 16th, 2011 at 1:58 am
Yes it should; actually, with a temporary table as the result, I don’t think you even need set FMTONLY off. But anything returned as a resulting select (temp tables and table variables included) will be generated.