Aeseir
Aeseir

Reputation: 8414

AddAsync() vs Add() in EF Core

Ok multiple questions here:

Upvotes: 88

Views: 51700

Answers (4)

lonix
lonix

Reputation: 20489

Other answers explain the technical difference between Add and AddAsync.

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

You probably don't use a HiLo generator, so ask yourself what'll happen five months from now when you realise there are two options, forget why, search the docs, search StackOverflow, etc.

So it's best to write idiomatic .NET code: for async code use AddAsync.

It improves readability. You won't encounter a block of async code that uses Add instead of AddAsync, thus leading to the above confusion loop.

Upvotes: -1

juunas
juunas

Reputation: 58723

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().

Upvotes: 35

snakey
snakey

Reputation: 894

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.

Upvotes: 86

sergeyxzc
sergeyxzc

Reputation: 615

It all depends on what you want?

AddAsync() makes sense to call if you are using an algorithm Hi/Lo. In all other cases, call sync Add().

It is important to understand that this is not some kind of EF feature, this is an old technique for working with a database. The choice of this technique has a profound effect on your data design. Therefore, it is more a matter of your approach to working with data, not sync-async code.

Here is a good description Hi/Li:

https://www.talkingdotnet.com/use-hilo-to-generate-keys-with-entity-framework-core/

What's the Hi/Lo algorithm?

Upvotes: 10

Related Questions