How can I delete 1,000 rows with EF6?

前端 未结 3 1388
余生分开走
余生分开走 2020-11-29 06:15

I am using Entity Framework 6.

I have a table with test information called Tests. I am deleting rows from this table by first getting a list of the tests, doing a

3条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 07:09

    Built in Entity Framework .RemoveRange() method, still Fetches the Entries on memory , and issues X deletes looping though all of them.

    If you don't want to write Any SQL for Deletion especially when selecting which entities to delete is complex

    Entity Framework Plus Library offers batch delete-update methods issuing only one single command.

    // Deleting
    context.Users
      .Where(u => u.FirstName == "firstname")
      .Delete();
    

    A current limitations of the Entity Framework is that in order to update or delete an entity you have to first retrieve it into memory. Now in most scenarios this is just fine. There are however some senerios where performance would suffer. Also, for single deletes, the object must be retrieved before it can be deleted requiring two calls to the database. Batch update and delete eliminates the need to retrieve and load an entity before modifying it.

提交回复
热议问题