使用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.
现在,我已将此函数(InsertNewUser)映射到我的实体框架,并按如下方式调用它:
Now I've mapped this function (InsertNewUser) to my Entity Framework and am calling it like so:
context.InsertNewUser(email, name, passwordhash, salt, ???)
???是我遇到麻烦的地方.在存储过程中,此参数是OUTPUT参数.我尝试声明一个字符串,然后传入"out definedString",但这是不正确的.
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?
这是存储过程:
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
推荐答案
我使用以下代码解决了该问题:
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);
这篇关于使用LINQ和Entity Framework获取存储过程输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!