2

As far as I know, it's not recommended to use AddAsync() in every situation. based on this Microsoft 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.

And a PrimaryKey is considered as a generated value, so it means that it's possible to use an async method.

Does it mean that whenever we have a [key] in an entity we can use AddAsync() to add it to the database?

user3748973
  • 449
  • 2
  • 8
  • 24
  • 2
    Well all entities compatible with the change-tracker must have a key defined in the net core db model so you can call .AddAsync on those entities (so not keyless entities ofc). so yes you definitely can - are you asking whether there are any negative impacts or why would you like to know this? – sommmen Jul 18 '22 at 09:59
  • @sommmen Yes, it would be good to know about the negative impacts. – user3748973 Jul 18 '22 at 10:08
  • Does this answer your question? [AddAsync() vs Add() in EF Core](https://stackoverflow.com/questions/47135262/addasync-vs-add-in-ef-core) – sommmen Jul 18 '22 at 12:15
  • In that case i'm voting to close as a dupe - since https://stackoverflow.com/a/52318741/4122889 answers this question. Basically: Yes use AddAsync in an Async context and use Add in a sync context - don't bother with the async overhead - optimize your queries will yield better results. – sommmen Jul 18 '22 at 12:16
  • @sommmen Honestly not completely, and I've seen that question before, I just wanted to make sure that is it possible to use async with all entities which have `[key]` or not. – user3748973 Jul 18 '22 at 12:54
  • 1
    Okay well - yes, just try it - it works great. I use a combination in my app and it works as it should. – sommmen Jul 18 '22 at 13:33

0 Answers0