Entity Framework returns Null

3

Good I have this store

@CodRegistro    NVARCHAR(20) = NULL,
@UsuarioWindows NVARCHAR(11) = NULL,
@CodPersonal    CHAR(5) = NULL,
@IP             NVARCHAR(11) = NULL,
@TipoMetodo     INT,
@Query          NVARCHAR(50) = NULL,
@Pagina         INT = NULL,
@Cantidad       INT = NULL,
@Total          INT = NULL OUT

DECLARE @Desde AS INT       --Páginado
DECLARE @Hasta AS INT       --Páginado
SET @Desde = ((ABS(@Pagina - 1) * @Cantidad) + 1);
SET @Hasta = @Desde + @Cantidad - 1;

SELECT 
            @Total = ISNULL(COUNT(*), 0) 
        FROM 
            Presupuesto.RelacionCodigoUsuario RCU
        WHERE (
            RCU.CodPersonal LIKE '%' + @Query + '%' OR
            RCU.UsuarioWindows LIKE '%' + @Query + '%'
        );

        WITH  ConsultaCore AS(
            SELECT ROW_NUMBER() OVER (ORDER BY RCU.CodRegistro DESC) AS NRegistro,
                RCU.CodPersonal,
                RCU.CodRegistro,
                RCU.UsuarioWindows,
                RCU.FechaInsert,
                RCU.FechaModif,
                RCU.IPInsert,
                RCU.IPModif,
                RCU.UsuarioInsert,
                RCU.UsuarioModif,
                RCU.CodPersonal personal, 
                Presupuesto.GetCC1(RCU.CodPersonal) cc1
            FROM 
                Presupuesto.RelacionCodigoUsuario RCU
            WHERE (
                RCU.CodPersonal LIKE '%' + @Query + '%' OR
                RCU.UsuarioWindows LIKE '%' + @Query + '%'
            )
        )
        SELECT 
            cc.NRegistro
            ,cc.CodPersonal
            ,cc.CodRegistro
            ,cc.UsuarioWindows
            ,cc.FechaInsert
            ,cc.FechaModif
            ,cc.IPInsert
            ,cc.IPModif
            ,cc.UsuarioInsert
            ,cc.UsuarioModif
            ,cc.personal
            ,cc.cc1
        FROM ConsultaCore cc 
        WHERE @Desde <= cc.NRegistro AND @Hasta >= cc.NRegistro 
        ORDER BY cc.NRegistro ASC;

Well the store is the least important and it works well in the sql when I send the parameters and everything, but from the Entity Framwork it returns the number of records well but all null.

This is my code

public static List<RelacionCodigoUsuario> Buscar(string Query, int Pagina, int Cantidad, ref int Total)
    {
        List<RelacionCodigoUsuario> data = new List<RelacionCodigoUsuario>();
        CostosEntities context = new CostosEntities();
        int TipoMetodo = 0;
        data = context.Database.SqlQuery<RelacionCodigoUsuario>("Presupuesto.sp_PersonalCentroCosto @CodRegistro, @UsuarioWindows, @CodPersonal, @IP, @TipoMetodo, @Query, @Pagina, @Cantidad, @Total",
            new SqlParameter("@CodRegistro", DBNull.Value),
            new SqlParameter("@UsuarioWindows", DBNull.Value),
            new SqlParameter("@CodPersonal", DBNull.Value),
            new SqlParameter("@IP", DBNull.Value),
            new SqlParameter("@TipoMetodo", TipoMetodo),
            new SqlParameter("@Query", Query),
            new SqlParameter("@Pagina", Pagina),
            new SqlParameter("@Cantidad", Cantidad),
            new SqlParameter("@Total", Total)).ToList();
        return data;
    }

And this is my class

public class RelacionCodigoUsuario
{
    public int NRegistro { get; set; }
    public string CodRegistro { get; set; }
    public string CodPersonal { get; set; }
    public string UsuarioWindows { get; set; }
    public string IPInsert { get; set; }
    public string IPModif { get; set; }
    public string UsuarioInsert { get; set; }
    public string UsuarioModif { get; set; }
    public DateTime FechaInsert { get; set; }
    public DateTime? FechaModif { get; set; }
}
    
asked by Gabriel Jeremy Rodriguez River 28.03.2017 в 18:04
source

1 answer

1

I would advise to implement a simple example of the procedure to validate its operation

ALTER PROCEDURE sp_PersonalCentroCosto
AS
BEGIN

    SELECT 
        0 as NRegistro,
        CodRegistro,
        CodPersonal,
        UsuarioWindows,
        IPInsert,
        IPModif,
        UsuarioInsert,
        UsuarioModif,
        FechaInsert,
        FechaModif
    FROM Presupuesto.RelacionCodigoUsuario 
    ORDER BY cc.NRegistro ASC;

END


public static List<RelacionCodigoUsuario> Buscar(string Query, int Pagina, int Cantidad, ref int Total)
{

    List<RelacionCodigoUsuario> data = null;

    using(CostosEntities context = new CostosEntities())
    {
        data = context.Database.SqlQuery<RelacionCodigoUsuario>("Presupuesto.sp_PersonalCentroCosto").ToList();
    }

    return data;
}

that the implementation is simple, but validates that this way it works, then you can add parameters and other complexity to see what causes the problem with the mapping of the fields

You will see that in the procedure respect the order of the properties in addition to returning exactly what you define in the classes (so remove the personal and cc1 of the select)

    
answered by 28.03.2017 в 20:08