9

I've created a stored procedure that takes parameters to create a user. If the user already exists it sets the output parameter to 'User already exists' and does nothing more.

Now I've mapped this function (InsertNewUser) to my Entity Framework and am calling it like so:

context.InsertNewUser(email, name, passwordhash, salt, ???)

The ??? is where I'm having trouble. In the stored procedure this parameter is an OUTPUT parameter. I tried declaring a string and then passing in "out declaredString" but that wasn't correct.

I'm not sure I'm going about this the right way, any thoughts?

This is the Stored Procedure:

ALTER PROCEDURE dbo.InsertNewUser

    (
    @eMail nvarchar(256),
    @firstName nvarchar(256),
    @lastName nvarchar(256),
    @passwordHash nvarchar(256),
    @salt nvarchar(256),
    @output nvarchar(256) OUTPUT
    )

AS
    /* Saves a user to the db. */
    BEGIN
    --First check if the user doesn't exist
    IF EXISTS (SELECT eMail FROM UserSet WHERE eMail = @eMail)  
        --Return that user exists
        SET @output = 'User exists' 
    ELSE    
        INSERT INTO UserSet
        VALUES (@eMail, @firstName, @lastName, @passwordHash, @salt)
    END

3 Answers 3

11

You can also write in the following way:

string output = "";    
context.InsertNewUser(email, name, passwordhash, salt, ref output)
Sign up to request clarification or add additional context in comments.

1 Comment

so.. much.. easier.. why didn't I think of ref?! Only tried out
6

I solved it with this code:

//This will provide the parameter
System.Data.Objects.ObjectParameter parameter = new ObjectParameter("output", "nvarchar(256)");
//This will contain the returned values
ObjectResult result = context.CheckIfUserExists(eMail, parameter);

Comments

1

I solved it with following code:

//Execute stored procedure
ObjectParameter newkey = new ObjectParameter("newKey", typeof(char));
db.RF_GetNewKey("A", "B", "S",newkey);

//get new key output from stored procedure RF_GetNewKey
string myKey=newkey.Value.ToString();

With Entity Framework 6

1 Comment

I have also implemented sequence to generate key but i can't do rollback if transaction fail. How do you do that ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.