Why Entity Framework have AddAsync?

.net c# entity-framework entity-framework-core

Question

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?

1
7
10/31/2018 11:21:43 AM

Accepted Answer

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?

10
10/31/2018 11:32:56 AM

Popular Answer

Begins tracking the given entity, and any other reachable entities that are not already being tracked, in the Added state such that they will be inserted into the database when SaveChanges() is called.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.addasync?view=efcore-2.1



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow