Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AddAsync() vs Add() in EF Core

Ok multiple questions here:

  • I am trying to understand what is the difference (outside the obvious asynchronous) between AddAsync() and Add() methods in EF Core?

  • When do you choose one over the other?

  • Does it matter if you choose one over the other for consistency?

like image 295
Aeseir Avatar asked Nov 06 '17 10:11

Aeseir


People also ask

What is AddAsync C#?

AddAsync(Object, CancellationToken) 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. Use State to set the state of only a single entity.

What is UseHiLo?

UseHiLo(PropertyBuilder, String, String) Configures the key property to use a sequence-based hi-lo pattern to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

Can I use Efcore with .NET framework?

You can use EF Core in APIs and applications that require the full . NET Framework, as well as those that target only the cross-platform .

What is scaffold DbContext?

Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema. It can be performed using the Scaffold-DbContext command of the EF Core Package Manager Console (PMC) tools or the dotnet ef dbcontext scaffold command of the . NET Command-line Interface (CLI) tools.


2 Answers

After going through the code I agree with Henk Holterman's comment that using Add() when your code is async is an optimization. The documentation for AddAsync() is a little misleading when it says, "For all other cases the non async method should be used".

I am trying to understand what is the difference (outside the obvious asynchronous) between AddAsync() and Add() methods in EF Core?

AddAsync() is 100% async safe, while Add() is only async safe in certain conditions. Like the comment implies, one of your columns may be configured such that Entity Framework makes a query to the database to generate the value that will eventually be inserted. In that case, blocking would occur if you called Add().

When do you choose one over the other?

  • If you're not writing async code, then definitely use Add().
  • If you're writing async code and want to keep things simple, choose AddAsync() just as you would for other methods.
  • If you really want to avoid the overhead of async calls and you know Add() will never make a database query, then use Add().

Does it matter if you choose one over the other for consistency?

No, despite the recommendation in the AddAsync() documentation.

like image 63
snakey Avatar answered Oct 04 '22 11:10

snakey


From the source code:

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.

So if you use a value generator that might need to access the DB to get new values to assign to new entries, such as the SequenceHiLo generator, then use AddAsync().

like image 27
juunas Avatar answered Oct 04 '22 10:10

juunas