Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DbSet does not have an async method for remove range?

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.

like image 349
Notbad Avatar asked Feb 11 '20 12:02

Notbad


People also ask

What is the use of the dbset removerange method?

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.

What does dbset mean?

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.

How do I add an entity to a dbset?

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.

When to use Async or non-async methods in Entity Framework?

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.


1 Answers

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.

like image 54
Ivan Stoev Avatar answered Oct 21 '22 03:10

Ivan Stoev