I have a stored procedure in SQL server 2008. It takes an email as parameter and then looks up if the user linked to it has rights to access a mobile MVC 5 application. The stored procedure returns either a 1 or a 0. Here is the code for the stored procedure.
-- Add the parameters for the stored procedure here
@email varchar(128)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @Check int
SELECT
@Check = (
SELECT P.MobileRights
FROM eMK_Person as P
WHERE P.ID = (SELECT FK_Person FROM eMK_contact WHERE eMK_Contact.Information = @Email)
)
IF (@Check is null) RETURN 0
RETURN (@Check)
END
GO
In the standard mvc5 code of the account controller I added a simple check on this stored procedure, if the outcome is 0 it should just return the view. Here is a the code that I added to the Login task action result:
var test = db.SP_MB_MobileRights(model.Email);
if (db.SP_MB_MobileRights(model.Email) == 0)
{
return View(model);
}
I have two test accounts. One that has the rights and one that does not. If I execute the stored procedure in the management studio with a query it works like it should, returning 0 for one and 1 for the other.
But when I debug the code the test variable always gets filled with -1.
Here is the code that MVC made for me when I added the stored procedure to the model.
public virtual int SP_MB_MobileRights(string email)
{
var emailParameter = email != null ?
new ObjectParameter("email", email) :
new ObjectParameter("email", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("SP_MB_MobileRights", emailParameter);
}
When I debug through it, I can clearly see that the emailParameter variable is filled with the email address: