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

26,321

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.

Share:
26,321
Martin Jeremic
Author by

Martin Jeremic

Updated on July 09, 2022

Comments

  • Martin Jeremic
    Martin Jeremic almost 2 years

    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?

  • Alexander Derck
    Alexander Derck about 7 years
    So AddAsync could fetch an auto-generated value (autoincrement key etc.) from the server without calling SaveChanges for example?
  • DavidG
    DavidG about 7 years
    @AlexanderDerck I believe it is used when you are using sequences as you can get a number from them before you insert into a table. You won't be able to get an auto-incremented key without saving changes first.