Pass Parameters to Store Procedure

Asked

Viewed 580 times

3

Follow the Search Function Code

public List<Pessoa> Buscar(string Nome)
{ 
    using(var db = new MyContext())
    {
        var Result = db.Database.SqlQuery<Pessoa>("EXEC SP_Busca_Cliente @Nome", Nome).ToList();
        return Result.ToList();
    }
}

QUESTION ---- I have tried using Sqlparameters only that returns the Error that is not passing the correct Parameter for the Stored Procedure Is there a Method to set this parameter to pass it to the Stored Procedure?

  • Hugo posts his class Pessoa and also post your Storedprocedure!

1 answer

5


There is yes, the correct is this syntax (using your code as an example):

var result = context.Database.SqlQuery<Pessoa>(
    "SP_Busca_Cliente @Nome", new SqlParameter("@Nome", nome));

You do not need the "Exec" before the name of the process, just put the name of the process and the list of parameters.

Source.

  • Cool, He Passed the Parameter only it still returns me an Error -- "+ $Exception {"The data Reader is incompatible with the specified 'Webapplication1.Models.Pessoa'. A Member of the type, 'Codigo', does not have a corresponding column in the data Reader with the same name." } System.Data.Entity.Core.Entitycommandexecutionexception" -- Does List() ?

  • No EXEC, Using the Same Way You Passed Me.

  • Ah, he’s saying that the result of his stored Procedure does not match the class Pessoa, your select na stored Previous has columns with different names of the person class.

  • Putz, Verdade, Vlew Agora It worked without errors, now just treat the View To Return to List, Vlew Same Old

  • You’re welcome, just accept my answer as the correct one.

Browser other questions tagged

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