如何从sql中的存储过程获取GUID到mvc

时间:2016-04-28 07:24:36

标签: c# sql entity-framework linq

对于我的应用程序,我创建了一个存储过程(取自here):

Create proc spResetPassword
@UserName nvarchar(100)
as
Begin
Declare @UserId int
Declare @Email nvarchar(100)

Select @UserId = Id, @Email = Email 
from tblUsers
where UserName = @UserName

if(@UserId IS NOT NULL)
Begin
--If username exists
 Declare @GUID UniqueIdentifier
 Set @GUID = NEWID()

 Insert into tblResetPasswordRequests
(Id, UserId, ResetRequestDateTime)
Values(@GUID, @UserId, GETDATE())

Select 1 as ReturnCode, @GUID as UniqueId, @Email as Email
End

我创建了一个模型并添加了所需的属性:

[Table("tblResetPasswordRequests")]
public class RequestResetPasswordModel
{
    [Key]
    public Guid Id { get; set; }
    [ForeignKey("ADMIN_ID")]
    public  AdminLogin  UserId{ get; set; }
    public DateTime ResetRequestDateTime { get; set; }
}

在控制器中,我在方法中调用了存储过程:

var uniqueIdForUser = db.Database.SqlQuery<RequestResetPasswordModel>("spResetPassword {0}", user.ADMIN_USERNAME).FirstOrDefault();

记录被插入到db表中,但是当我调试方法时,Id没有被传递,它每次只显示00000000-0000-0000-0000-000000000000。我该怎么办?请帮助< / p>

1 个答案:

答案 0 :(得分:1)

您可以将proc的最后一行更改为

SELECT @GUID AS Id, @UserId AS UserId

尝试一下...... 我不确定,但情况可能就是这样......