Example #1
0
        /// <summary>
        /// Creates necessary database tables and seeds initial data.
        /// </summary>
        private async Task InitializeDatabase()
        {
            using (var context = new MeterReaderContext()) {
                DatabaseFacade            db = context.Database;
                RelationalDatabaseCreator databaseCreator = (RelationalDatabaseCreator)db.GetService <IDatabaseCreator>();

                try {
                    databaseCreator.Create();
                }
                catch (Exception e) {
                    // Database already exists
                }
                try {
                    databaseCreator.CreateTables();
                }
                catch (Exception e) {
                    // Tables already exist
                }

                using (StreamReader accountsCsv = new StreamReader(CUSTOMER_CSV_FILE_PATH)) {
                    CustomerAccountCsvParser         csvParser = new CustomerAccountCsvParser();
                    CsvParseResult <CustomerAccount> accounts  = await csvParser.ParseCsvFileAsync(accountsCsv);

                    CustomerAccountDbTableInterface accountsTable = new CustomerAccountDbTableInterface();
                    await accountsTable.InsertEntriesAsync(accounts.Data);
                }
            }
        }
        /// <summary>
        /// Extract data from a CSV file and populate the associated database table.
        /// </summary>
        /// <param name="csvFile">TextReader object which reads in the contents of the CSV file.</param>
        /// <returns>CsvUploadResult detailing the number of entries which succeeded and failed to load.</returns>
        public async Task <CsvUploadResult> UploadCsvFileAsync(TextReader csvFile)
        {
            CsvParseResult <MeterReading> parseResult = await MeterReadingParser.ParseCsvFileAsync(csvFile);

            DbResult insertResult = await MeterReadingTable.InsertEntriesAsync(parseResult.Data);

            return(new CsvUploadResult {
                RowInsertCount = insertResult.InsertCount,
                ErrorCount = parseResult.ErrorCount + insertResult.ErrorCount
            });
        }