locked
Approach to batch delete or update in Entity Framework Core 5 RRS feed

All replies

  • User-2054057000 posted

    Entity Framework core already have RemoveRange feature which does this thing. Ex of code as shown below:

    List<Department> dept = new List<Department>()
    {
        new Department(){Id=1},
        new Department(){Id=2},
        new Department(){Id=3}
    };
     
    using (var context = new CompanyContext())
    {
        context.RemoveRange(dept);
        await context.SaveChangesAsync();
    }

    Friday, November 27, 2020 5:02 AM
  • User-1643914890 posted

    Entity Framework core already have RemoveRange feature which does this thing. Ex of code as shown below:

    List<Department> dept = new List<Department>()
    {
        new Department(){Id=1},
        new Department(){Id=2},
        new Department(){Id=3}
    };
     
    using (var context = new CompanyContext())
    {
        context.RemoveRange(dept);
        await context.SaveChangesAsync();
    }

    EF core will delete them one by one, which means three delete SQL Statements will be executed.

    In contrast, using my library, only a single SQL

    delete from b where id=1 or id=2 or id=3

    will be executed.

    Saturday, November 28, 2020 3:28 PM
  • User475983607 posted

    Your approach only works if the Ids are contiguous.  Seems an unlikely scenario especially after executing the first delete batch. 

    Saturday, November 28, 2020 3:49 PM
  • User-1643914890 posted

    Your approach only works if the Ids are contiguous.  Seems an unlikely scenario especially after executing the first delete batch. 

    You misunderstood my library. It supports all the lambda expression which EF Core supports. So, with it, you can perform any filter condition.

    Sunday, November 29, 2020 10:48 AM
  • User1655654435 posted

    great work. I've been looking for something like this. I rarely see anything about multiple update or deletes in the documentation or tutorials to ef core for some reason.

    Monday, November 30, 2020 2:21 PM