简体   繁体   English

使用输出参数调用Entity Framework过程时出错

[英]Error calling Entity Framework procedure with output parameter

I am attempting to Salt my Passwords. 我正在尝试添加密码。

Here is my sproc which returns my pw as an nvarchar(512): 这是我的存储程序,它将我的pw作为nvarchar(512)返回:

alter proc [dbo].g_GetPWFromEmail
@emailAddress nvarchar(50),
@EncrPass nvarchar(512) OUTPUT

as
if ((Select count(*) from users  where emailAddress = @emailAddress) > 0)
begin
    select @EncrPass =  password 
    from users 
    where emailAddress = @emailAddress
end
else
begin
    select @EncrPass =  ''
end

Here is what was scaffolded in the emdx file: 这是在emdx文件中安装的支架:

public virtual ObjectResult<string> g_GetPWFromEmail(string emailAddress, ObjectParameter encrPass)
{
    var emailAddressParameter = emailAddress != null ?
        new ObjectParameter("emailAddress", emailAddress) :
        new ObjectParameter("emailAddress", typeof(string));

    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<string>("g_GetPWFromEmail", emailAddressParameter, encrPass);
}

And when I call it it errors out... not sure how to call this - here is what I have so far: 当我称它为错误时...不知道如何称呼它-这是我到目前为止所拥有的:

//Pull Current Encrypted Password from DB
string DbPw = "";
ObjectParameter DBPW1 = null;
var DbPassWord =  databaseManager.g_GetPWFromEmail(model.Username, DBPW1).ToList();
if (DbPassWord != null && DbPassWord.Count() > 0)
{
     DbPw = Convert.ToString(DBPW1);
}

How do I call this to return my hashed pw from the DB? 我如何称呼它从数据库返回我的散列密码? I know there is a better way, but have tried several ways and can't get the syntax quite right. 我知道有一个更好的方法,但是尝试了几种方法并且无法获得正确的语法。

If your use case is really this simple, then regular LINQ-to-Entities will work, assuming you've set up your tables as entities: 如果您的用例确实如此简单,那么假设您已将表设置为实体,则常规LINQ-to-Entities将起作用:

using (var context = new MyContext()) {
    string encryptedPassword = context.Users
        .Where(u => u.emailAddress == emailAddress)
        .Select(u => u.password)
        .SingleOrDefault();
}

You pass in a null reference ( DPBW1 ) into the method. 您将一个空引用( DPBW1 )传递给该方法。 Therefore, the method cannot update that reference with the output value. 因此,该方法无法使用输出值更新该引用。

ObjectParameter DBPW1 = null;
var DbPassWord =  databaseManager.g_GetPWFromEmail(model.Username, DBPW1).ToList();

I haven't done this in EF, but in SQL ADO.NET, you still create the parameter and just mark the parameter direction as Output, then after the proc runs, the parameter value is populated. 我没有在EF中完成此操作,但是在SQL ADO.NET中,您仍然创建参数并将参数方向标记为Output,然后在proc运行后填充参数值。

You should be able to do the same thing here. 您应该可以在这里做同样的事情。

EDIT Looks like ObjectParameter doesn't define the parameter direction. EDIT看起来ObjectParameter没有定义参数方向。 Maybe it's done automatically - I'd have to try it to see if it works. 也许它是自动完成的-我必须尝试一下以查看它是否有效。

var DPBW1 = new ObjectParameter("EncrPass", typeof(string));

EDIT It does work without setting the parameter direction 编辑它确实可以在不设置参数方向的情况下工作

暂无
暂无

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

相关问题 使用带有 output 参数的实体框架调用 Oracle 存储过程? - Calling Oracle stored procedure using Entity Framework with output parameter? 从实体框架调用存储过程时出错 - Error calling stored procedure from Entity Framework 在Entity Framework代码优先中使用输出参数调用存储过程时出错 - Error on calling stored procedure with output param in Entity Framework code-first 使用Entity Framework调用存储过程时出现问题:期望参数未提供 - Problems calling a stored procedure using Entity Framework: expects parameter not supplied 实体框架5 - 使用参数调用存储过程。 参数无法识别 - Entity Framework 5 - Calling a stored procedure with parameters. Parameter not recognized 使用参数和多个结果集实体框架调用存储过程6 - Calling Stored Procedure with Parameter and Multiple Result Set Entity Framework 6 实体框架调用存储过程需要未提供的参数 - Entity Framework calling stored procedure expects parameter which was not supplied 使用 Entity Framework 获取存储过程 output 参数会引发映射错误:数据读取器与指定的不兼容 - Getting stored procedure output parameter with Entity Framework is throwing a mapping error: The data reader is incompatible with the specified 使用LINQ和Entity Framework获取存储过程输出参数 - Getting stored procedure output parameter with LINQ and Entity Framework 实体框架核心2中具有输出参数的Exec存储过程 - Exec Stored Procedure with output parameter in Entity Framework Core 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM