Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there DBSet.UpdateAsync() and RemoveAsync() in .net core?

I could not find any info on this anywhere.

There are ToListAsync(), AddAsync() and more, but could not find any documentation about UpdateAsync() or RemoveAsync().

Does anyone know anything about this?

like image 566
Martin Jeremic Avatar asked Feb 03 '17 22:02

Martin Jeremic


People also ask

What is DbSet in asp net core?

In Entity Framework Core, the DbSet represents the set of entities. In a database, a group of similar entities is called an Entity Set. The DbSet enables the user to perform various operations like add, remove, update, etc. on the entity set.

When to use async EF Core?

In general, one would use async await when you want to keep the currently running thread from blocking. This frees the thread for other tasks (like updating the UI), while we await the asynchronous result.

Is LINQ asynchronous?

Note that there are no async versions of some LINQ operators such as Where or OrderBy, because these only build up the LINQ expression tree and don't cause the query to be executed in the database. Only operators which cause query execution have async counterparts.


1 Answers

ToListAsync exists because it actually causes EF to head off to the data store to retrieve the data. This may take some time, hence why you can call it asynchronously.

AddAsync however, only begins tracking an entity but won't actually send any changes to the database until you call SaveChanges or SaveChangesAsync. You shouldn't really be using this method unless you know what you're doing. The reason the async version of this method exists is explained in the docs:

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.

Update and Remove are the same as Add in as much as they only affect the internal tracking until you save the changes you've made.

like image 68
DavidG Avatar answered Sep 19 '22 08:09

DavidG