[英]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.