I can't find an async version of RemoveRange. But, it exists for excample for AddRange. Do anybody know why is it? It seems odd to me not to have an homogeneous set of commands.
The DbSet RemoveRange method is used to remove or delete a collection of entities from the database. Like the Remove method, when the RemoveRange method is called the entities are not removed immediately rather the entity’s states will be marked as deleted.
DbSet AddRange Method in Entity Framework: The DbSet AddRange method is used to add a given collection of entities to the context with the Added State. When the changes are saved (i.e. when the SaveChanges method is called on the Context object), all the entities which are in the Added state are inserted into the database table.
The DbSet Add method is used to add the given entity to the context with the Added State. When the changes are saved (i.e. when the SaveChanges method is called), the entity which is in the Added state is inserted into the database.
This method is async only to allow special value generators, such as the one used by 'Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo', to access the database asynchronously. For all other cases the non async method should be used.
Because it is synchronous operation and providing fake Async
method which runs synchronously and returns completed task would be misleading and against async method principles.
EF Core provides async versions only for methods which potentially access database - e.g. Add{Range}
, Find
, SaveChanges
, Dispose
, and sync only version for methods which operate purely on state (change tracker) like Attach{Range}
, Update{Range}
, Remove{Range}
.
As of why Add{Range}
have async version, the reason is explained in the documentation:
This method is async only to allow special value generators, such as the one used by
Microsoft.EntityFrameworkCore.Metadata.SqlServerValueGenerationStrategy.SequenceHiLo
, to access the database asynchronously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With