Delete multiple database elements

Asked

Viewed 133 times

3

I have a action [GET] delete on my Webapi.

She removes one element, but would like it to remove all the elements that have a particular code.

How can I change my code to remove all?

My Code:

[HttpGet]
[Route("delete/{Con_codigo}")]
public HttpResponseMessage delete(int Con_codigo)
{
    try
    {
        var condel = bdprincipalEntities.Contatos
                         .FirstOrDefault(x => x.Con_codigo == Con_codigo);

        bdprincipalEntities.Contatos.Remove(condel);
        bdprincipalEntities.SaveChanges();

        var result = new HttpResponseMessage(HttpStatusCode.OK);
        return result;
    }
    catch (Exception)
    {
        return new HttpResponseMessage(HttpStatusCode.BadRequest);
    }
}

I know that my search returns only the first. I thought of changing from FirstOrDefault for Select, but he gives me the following error message:

Unable to convert from "System.linq.Iqueryable to Softluxapi.Models.

Updating:

The above method ends up removing all contacts, as I was asking, so I’m changing the question to, because even with the FirstOrDefault it removes various elements from the database?

  • An important tip: Don’t change the focus of your question after posting it, especially if the post has already received answers. You will end up causing your question to get confused and the respondents lose the job they had to write an answer. If you have a different question and it is pertinent to ask here, open a new question, there is no limit to posts.

1 answer

3


Before anything I need to say have a GET endpoint to remove a resource doesn’t make the slightest sense semantically.


If you are using EF 6.0 or a newer version, you can use the method RemoveRange and simplify the code.

bdprincipalEntities.Contatos.RemoveRange(
    bdprincipalEntities.Contatos.Where(x => x.Con_codigo == Con_codigo));

If you use an older version, you will need to fetch all entities and then delete them one by one.

var contatos = bdprincipalEntities.Contatos.Where(x => x.Con_codigo == Con_codigo);

foreach(var contato in contatos)
    bdprincipalEntities.Contatos.Remove(contato);

bdprincipalEntities.SaveChanges();
  • I can use the Removerange method, but I didn’t know I could make queries this way, I’m new in the language, thank you very much for the answer, but can I have another question before I accept your answer? because even with Firstordefault it removes various elements from the database?

  • @Jeffhenrique I have no way to answer without knowing more details. But it’s probably because the column Con_codigo has repeated values and the generated SQL is using this value in the Where clause.

  • Yes, it has repeated values as it is necessary for the project, but very obg, helped mt

  • Regarding the GET endpoint, I took the project and it was like this before, but I’m going to talk here about not making sense semantically

Browser other questions tagged

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