Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Entity Framework have AddAsync?

I understand why EF have ToListAsync or SaveChangesAsync - because it waits for db operation execution. But AddAsync just returns Task.FromResult - so why is there the AddAsync method? And why should I use it?

like image 983
Alex Zaitsev Avatar asked Feb 03 '23 21:02

Alex Zaitsev


1 Answers

From 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. For all other cases the non async method should be used.

SqlServerValueGenerationStrategy.SequenceHiLo:

A sequence-based hi-lo pattern where blocks of IDs are allocated from the server and used client-side for generating keys.

See also What's the Hi/Lo algorithm?

like image 161
Rui Jarimba Avatar answered Feb 06 '23 12:02

Rui Jarimba