使用ef运行存储过程-不返回任何结果,但在ssms中工作

本文关键字:结果 但在 ssms 工作 任何 返回 运行 ef 存储过程 使用 | 更新日期: 2023-09-27 18:17:51

Store procedure:
    ALTER PROCEDURE [dbo].[UserRecordGet]
                @Name varchar(20) = null,
                @ID int = 0,
                @Email nvarchar(50) = null
    AS
    BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
        -- Insert statements for procedure here
    SELECT [id]
          ,[Username]
          ,[Password]
          ,[Type]
          ,[Role_id]
          ,[EmployeeID]
          ,[IsActive]
          ,[HomeTelephoneNumber]
          ,[Company_Alias]
          ,[AgentID]
          ,[LastLogin]
          ,[IsApproved]
          ,[LastPasswordChange]
          ,[FailedLogins]
          ,Application_Users.LockoutEnabled
          ,[OLBAccess]
          ,[MartinAccess]
          ,[MarbleAccess]
          ,[Email]
      FROM [Martin].[dbo].[Application_Users]
      WHERE
            ((@ID >0) AND id = @ID) OR
            ((@Name IS NOT null) AND Username = @Name COLLATE SQL_Latin1_General_CP1_CI_AS) OR
            ((@Email IS NOT NULL) AND Email= @Email)
    END
c#:

 public Task<MAUser> FindByIdAsync(int userId)
 {
     object[] parameters =  {
                                new SqlParameter("@Name",SqlDbType.VarChar,20){  Value = DBNull.Value, Direction = ParameterDirection.Input },
                                new SqlParameter("@Email",DBNull.Value){ Direction = ParameterDirection.Input, SqlDbType = SqlDbType.NVarChar,Size=50},
                                new SqlParameter("@Id",Convert.ToInt32(userId)){  SqlDbType.Int,Direction = ParameterDirection.Input}
                            };
     context = new MALoginContext();
     var user = context.Database.SqlQuery<MAUser>("exec dbo.UserRecordGet @Name, @Email, @Id", parameters).FirstOrDefault();
     return Task.Run<MAUser>(() => { return user; });
 }

但是我从那个方法返回null。

但是下面返回一个MAUser:

 public Task<MAUser> FindByNameAsync(string userName)
     {
         object[] parameters =  {
                                    new SqlParameter("@Name",SqlDbType.VarChar,20){ Value = userName, Direction = ParameterDirection.Input },
                                    new SqlParameter("@Email",DBNull.Value){ Direction = ParameterDirection.Input, SqlDbType = SqlDbType.NVarChar,Size=50},
                                    new SqlParameter("@Id",SqlDbType.Int){ Value=-1,Direction = ParameterDirection.Input}
                                };
            context = new MALoginContext();    
         var user = context.Database.SqlQuery<MAUser>("exec dbo.UserRecordGet @Name, @Email, @Id", parameters).FirstOrDefault();
         return Task.Run<MAUser>(() => { return user; });
     }

使用ef运行存储过程-不返回任何结果,但在ssms中工作

我想没有人发现我的错误。

线索在存储过程中。我把参数搞错了。真是个白痴。

我花了很多很多时间在那上面!!

相关文章: