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

DRW
  • 335
  • 1
  • 3
  • 17
  • 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 ? – Caius Jard Oct 28 '20 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) – Caius Jard Oct 28 '20 at 18:34
  • Does this answer your question? [How do I delete multiple rows in Entity Framework Core?](https://stackoverflow.com/questions/41960215/how-do-i-delete-multiple-rows-in-entity-framework-core) – Michael Freidgeim May 29 '22 at 08:07

2 Answers2

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();
Serge
  • 40,935
  • 4
  • 18
  • 45
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/223797/discussion-on-answer-by-serge-asp-net-core-3-1-and-entity-framework-core-delete). – Samuel Liew Oct 29 '20 at 06:38
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?
}
Svyatoslav Danyliv
  • 21,911
  • 3
  • 16
  • 32