1

I am working on a Vue + ASP.NET Core 3.1 with OData endpoint app and I'm working out how to delete items. So far I can delete a single item in the backend. I am still quite new to all this just looking for a bit of guidance on the issue.

Now I need to set it up to delete multiple selected items that is sent via an axios call to the backend. I am having trouble setting up the delete method to delete multiple files as well as testing it in postman

This is the code I have so far

[HttpDelete("{key}")]
public async Task<ActionResult<Asset>> DeleteAsset(List<int> key)
{
    var asset = await _context.Assets.FindAsync(key);
    var assets = _context.Assets.ToList().Where(a => a.Id == 0);

    if (asset == null)
    {
        return NotFound();
    }

    _context.Assets.Remove(asset);
    await _context.SaveChangesAsync();

    return asset;
}

How can I set this up to accept multiple Id's sent from the front end in an axios.delete call and have it delete those items from the database?

I know I can also use .RemoveRange() but again cannot figure out how to get it set up with out it giving me a conversion error on the return

I also would like to know how I can test this method using postman?

Any guidance would be appreciated .

Update:

This is my endpoint configuration in startup.cs

 app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller}/{action=Index}/{id?}");
                endpoints.MapODataRoute("odata", "odata", GetEdmModel(app.ApplicationServices), batchHandler: new DefaultODataBatchHandler());
                endpoints.Select().Expand().OrderBy().Filter().Count().MaxTop(null);
            });

When I have the action setup for single item delete it works, When I do it for the multiple delete It does not even fire. I just get the dead error in postman

3
  • You load a single asset and then delete it (remove it from the Assets collection). You also (i presume successfully) load a collection assets which could, potentially, have multiple items in it (if a.Id is unique, pick something else to Where if you want to load multiple entities).. And you do nothing with it. So my question is; how do you imagine you might go about deleting all the items in the assets collection, based on your existing successful pattern of removing a single asset ? Commented Oct 28, 2020 at 18:29
  • pps; I would avoid doing _context.Assets.ToList() unless you want to download every item in the db's Assets table and then search through them locally (almost never a good idea) Commented Oct 28, 2020 at 18:34
  • Does this answer your question? How do I delete multiple rows in Entity Framework Core? Commented May 29, 2022 at 8:07

2 Answers 2

1

Try this:

try
{
var assets =  await _context.Assets.Where(a => key.Contains(a.Id)).ToArrayAsync();

    if (assets == null)
    {
        return NotFound();
    }

   _context.Assets.RemoveRange(assets);
 var result=   await _context.SaveChangesAsync();
if(result==0) return BadRequest(" delete error");
}
catch(Exeption ex)
{

 return BadRequest (ex.Message);

}
return Ok();
Sign up to request clarification or add additional context in comments.

1 Comment

Comments are not for extended discussion; this conversation has been moved to chat.
0

Try this approach, I think should work

[HttpDelete("{key}")]
public async Task<ActionResult<Asset>> DeleteAsset(List<int> key)
{
    var assets = key.Select(id => new Asset { Id = id });

    _context.Assets.RemoveRange(assets);
    await _context.SaveChangesAsync();

    return // what to return, we have removed a lot of assets?
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.