Storing and updating entity count in database