How to Count the Number of records in the Database Table using Asp.Net Core Entity Framework Core



Question: How do you get a Count of records in the Database Table using the Asp.Net Core Entity Framework Core?

Answer: You can easily get the count of elements or records in the Database Table using Entity Framework by utilizing the CountAsync() function on the Database Context. Keep in mind the best practices of requesting data from the Database, always include a Where Clause. 

See the Code below for reference:

int count = await _context.YourModalClass.CountyAsync<YourModelClass>(a => a.YourPredicateComesInHere == YourValueCouldBeAnID).ConfigureAwait(false); 

 

[NB] The ConfigureAwait(false) tells the EF Core to run this query on a different thread and then notify the UI Thread when the query completes executing.

Get Count of Data using Entity Framework Core Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue


© 2021 - ErnesTech - Privacy