Delete api not working, do not delete. Stored Procedure in the database is working

Asked

Viewed 42 times

-2

This is the API

public class DeleteCidade
    {
        BancoContext banco = new BancoContext(); 
        public void deleteCidade(int id)
        {
            banco.Database.SqlQuery<Cidade>("exec sp_del_cidade", new SqlParameter("@id", id));
        }          
    }

My cobtroller

[RoutePrefix("api/[controller]")]
    public class DeleteCidadeController : ApiController
    {
        DeleteCidade deleta = new DeleteCidade();

        [AcceptVerbs("Delete")]
        public void deleteCidade(int id)
        {
            deleta.deleteCidade(id);
        }
    }

as I call it in the Postman

http://localhost:55080/api/DeleteCidade/27

My SP

ALTER PROCEDURE [dbo].[sp_del_cidade] @id int
AS
BEGIN
  SET NOCOUNT ON
    delete from cidade
    where id = @id
END

when running by API(Postman) do not delete. No error, but do not delete.

The database is sql server. When Proc is run by Sql Server, it works fine.

  • Debugging, you’re falling for this API action?

  • Yes, I fall into the controller and then I give an F11 and I go to the method and the parameter is coming right

  • 1

    Changes the code to banco.Database.ExecuteSqlCommand("exec sp_del_cidade", new SqlParameter("@id", id)); as I’m not sure I’ll leave comment, if I resolve I reply

  • I have it: System.Data.SqlClient.SqlException: 'O procedimento ou a função 'sp_del_cidade' espera o parâmetro '@id', que não foi fornecido.' for this purpose: banco.Database.ExecuteSqlCommand("exec sp_del_cidade", new SqlParameter("@id", id));

  • Changes the code to banco.Database.ExecuteSqlCommand("exec sp_del_cidade @id", new SqlParameter("@id", id)); try again, actually do two tests, the one before and this: banco.Database.SqlQuery<Cidade>("exec sp_del_cidade @id", new SqlParameter("@id", id));, confirm if both or only 1 works, please.

  • @Barbetta, put on the last one I mark. It worked.

  • with Sqlquery worked?

  • @Barbetta, I didn’t even test anymore. I’m late in what I screwed up.

  • @Barbetta, for the Post is giving the same parameter error. I did similar to delete, but pointing to the SP insertion.

Show 5 more comments

1 answer

1


Has two problems, i) the command SqlQuery is to return entities, in case you are trying to execute an action. ii) is not being informed the variable after the `Procedure.

change to:

banco.Database.ExecuteSqlCommand("exec sp_del_cidade @id", new SqlParameter("@id", id));

Browser other questions tagged

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