locked
How to correctly delete a list of tables in EF Core? RRS feed

  • Question

  • User1655654435 posted

    Hi, I have a method which needs to delete a list of entities in the db. So far i only have had to delete just one at a time.

    The tables have also one to many to another table that i assume also will get deleted by default.

    I see I can just get them and then use _context.Remove(listOfTables); But I can get this to work. It throws an exception.

    but, so far i've used this code when removing entities, checking if they are in a detached state before attaching and then removing them. I assume this ensures that no one in the application can make changes to them as they are deleted? Given a list of tables, i would assume that detaching and setting them like this would be even more relevant?

                var avdeling = await _context.KlimaAvdeling.FindAsync(id);
                if (avdeling == null)
                {
                    throw new Exception("fant ikke avdeling i databasen");
                }
    
                if (_context.Entry(avdeling).State == EntityState.Detached)
                {
                    dbSet.Attach(avdeling);
                }
                dbSet.Remove(avdeling);

    I could just loop though the list and do this for each one. but my spider sense tells me this probably is not how you are suppose to do it. What if you have a big application with many users who are constantly interacting with your entities, how do you make sure the deletes/updates are done without anyone making changes to them while you do this?

    Friday, August 23, 2019 7:01 AM

Answers

  • User-821857111 posted

    Actually I can't get the _context.Remove(listOfEntities) to work. It throws an exception. It only works for single entities for some reason
    That's how it was designed to work. There is no RemoveAll or equivalent method. There's nothing wrong with your C# code looping through a collection and marking each as deleted. EF Core will generate a DELETE statement for each one. AFAIK, EF Core doesn't ever generate a batch delete statement eg. DELETE From YourTable Where Id In (......).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2019 7:36 AM

All replies

  • User-821857111 posted

    bluMarmalade

    so far i've used this code when removing entities, checking if they are in a detached state before attaching and then removing them. I assume this ensures that no one in the application can make changes to them as they are deleted?

    No it doesn't. It ensures that the context is tracking the entity to be deleted. Other concurrent users will have their own instance of the context and their own instance of the entity. 

    bluMarmalade

    how do you make sure the deletes/updates are done without anyone making changes to them while you do this?

    You are talking about concurrency. With deletes, it doesn't matter. You will discard all changes along with the entity anyway. If you want to know more about managing concurrency ion EF Core, see here: https://www.learnentityframeworkcore.com/concurrency

    bluMarmalade

    The tables have also one to many to another table that i assume also will get deleted by default.


    That depends on how the relationship was configured: https://www.learnentityframeworkcore.com/dbset/deleting-data#related-data

    Friday, August 23, 2019 7:15 AM
  • User1655654435 posted

    Actually I can't get the _context.Remove(listOfEntities) to work. It throws an exception. It only works for single entities for some reason. The exceptions reads: "the entity type List<KlimaAvdeling> was not found. Ensure that the entity type has been added to the model"

    Friday, August 23, 2019 7:27 AM
  • User-821857111 posted

    Actually I can't get the _context.Remove(listOfEntities) to work. It throws an exception. It only works for single entities for some reason
    That's how it was designed to work. There is no RemoveAll or equivalent method. There's nothing wrong with your C# code looping through a collection and marking each as deleted. EF Core will generate a DELETE statement for each one. AFAIK, EF Core doesn't ever generate a batch delete statement eg. DELETE From YourTable Where Id In (......).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2019 7:36 AM