My stored procedure implementation is simple:
create Procedure [dbo].[GetNegotiationInfo] (
@negotiationId uniqueidentifier
) As
select NegotiationId, SellerId from NegotiationMaster where negotiationId = @negotiationId
Then I expected to be to write the following code after updating my model.
using (var db = new NegotiationContext())
{
var query = db.GetNegotiationInfo(NegotiationId).FirstOrDefault();
}
But I am getting the error "int does not contain a definition for FirstOrDefault()". I know that Entity Framework can have trouble generating complex stored procedures with temp tables etc but obviously my stored procedure doesn't get any simpler than this. Please note my stored procedure is this basic for my StackOverflow question and is not the actual stored procedure I will be using.
Use
SET NOCOUNT ON
at the top of your procedure, the integer being returned is almost certainly the row count.Like so:
Stored procedures always return an integer. This is the status of the stored procedure call and will be set to
NULL
if not specified in the code. The general approach is that0
indicates success and any other value indicates an error. Here is some documentation on the subject.If you want to return a value from a stored procedure, use
output
parameters.Alternatively, you might want a user defined function which returns a value to the caller. This can be a scalar or a table.
EDIT:
I would suggest that you look into user defined functions.
In SQL, you would call this as:
EDIT (in response to Aaron's comment):
This discussion is about SQL-only stored procedures. Entity Framework wraps stuff around stored procedures. The documentation mentioned in the comment below strongly suggests that EF should be returning the data from the last select in the stored procedure, but that you should be using
ExecuteFunction
-- confusingly even though this is a stored procedure. (Search for "Importing Stored Procedures that Return Types Other than Entities" in the document.)Its all about editing the "Function Import" in the Visual Studio Model Browser and creating a complex type for the return record set. Note, temp Tables in your stored procedure will be difficult for Entity Framework to inspect. see MSDN