-1

I have an important question which I couldn't find the answer anywhere! Suppose we call the Count or CountAsync method in EF or EFCore like this:

public async Task<int> Count()
{
    return await _context.Set<TEntity>().CountAsync();
}

Does it results to save or add the entities into the database or not?

Thank you.

Sina Bahmanpour
  • 49
  • 2
  • 12
  • 2
    _"Does it results to"_ <-- I don't understand what you're asking. `Count`/`CountAsync` doesn't cause any changes to data, so what "save and add" actions are you referring to? – Dai Oct 24 '22 at 05:58
  • 2
    "does it take a lot of time to return the response" - only you can quantify what is or is not "a lot of time" and you can best assess this by *running the code* and measuring whether the response time is acceptable for your use case. – Damien_The_Unbeliever Oct 24 '22 at 05:59
  • 2
    _"And does it take a lot of time to return the response (for big data) or not?"_ - that depends entirely on your database design, network latency, how good your indexes are, database server disk IO speed, etc. – Dai Oct 24 '22 at 05:59
  • You could check this document:https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.queryableextensions.countasync?view=entity-framework-6.2.0 – Ruikai Feng Oct 24 '22 at 07:48

1 Answers1

0

Does it results to save or add the entities into the database or not?

No. Queries never write to the database. Only SaveChanges writes to the database.

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67