Refactored to write out entity map and raw table into DB