Entityframework: There is no mapping of the Objectparameter object type to a native type managed provider

Asked

Viewed 1,012 times

0

I am developing an MVC application with Entity Framework in Code First.

As my database has procedures, I opted to use Sqlquery to execute the procedures, but I am thinking about this error:

 Não existe mapeamento do tipo de objeto System.Data.Entity.Core.Objects.ObjectParameter para um provedor gerenciado de tipo nativo.

Follows code from the Dbcontext class:

public DTODados GetDadosProcedure(string codUsuario, string senhaCript)
{
    return Database.SqlQuery<DTODados>("ProcedureGenerica", 
        codUsuario != null ?
            new ObjectParameter("codUsuario", codUsuario) :
            new ObjectParameter("codUsuario", typeof(string)), 
        senhaCript != null ?
            new ObjectParameter("senhaCript", senhaCript) :
            new ObjectParameter("senhaCript", typeof(string))
    ).SingleOrDefault();
}

Following this link, I tried to switch to Sqlparameter, but when executing, I threw another exception stating that the process expects the parameters, even if they were present (I tried to put the new SqlParameter("@codUsuario", codUsuario) and without @, but it didn’t work).

Grateful to those who can help.

1 answer

0


Solved: I changed to Sqlparameters and correctly set the parameters:

public DTODados GetDadosProcedure(string codUsuario, string senhaCript)
{
    return Database.SqlQuery<DTODados>("ProcedureGenerica @codUsuario , @senhaCript", 
        new SqlParameter("@codUsuario", codUsuario),
        new SqlParameter("@senhaCript", senhaCript) 
    ).SingleOrDefault();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.