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?

Login to See the Rest of the 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.

© 2024 - ErnesTech - Privacy
E-Commerce Return Policy