简体   繁体   English

使用LINQ和Entity Framework获取存储过程输出参数

[英]Getting stored procedure output parameter with LINQ and Entity Framework

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: 现在我已将此函数(InsertNewUser)映射到我的实体框架,并调用它如下:

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

The ??? ??? is where I'm having trouble. 是我遇到麻烦的地方。 In the stored procedure this parameter is an OUTPUT parameter. 在存储过程中,此参数是OUTPUT参数。 I tried declaring a string and then passing in "out declaredString" but that wasn't correct. 我尝试声明一个字符串,然后传入“out declaredString”,但这不正确。

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

You can also write in the following way: 您也可以通过以下方式编写:

string output = "";    
context.InsertNewUser(email, name, passwordhash, salt, ref output)

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);

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 使用Entity Framework 6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM