public async Task Update(StocksContext stocksContext) { var content = await _companyInformationStore.GetFromStore(); var lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); if (lines.Length > 3) { for (var i = 3; i < lines.Length; i++) { // https://stackoverflow.com/questions/18144431/regex-to-split-a-csv var matches = Regex.Matches(lines[i], @"(?:^|,)(?=[^""]|("")?)""?((?(1)[^""]*|[^,""]*))""?(?=,|$)"); if (matches.Count != 3) { continue; } var stock = stocksContext.Stock.FirstOrDefault(s => s.Code == matches[1].Groups[2].Value); if (stock == null) { stock = new Model.Stock(); stocksContext.Add(stock); } stock.CompanyName = matches[0].Groups[2].Value; stock.Code = matches[1].Groups[2].Value; stock.IndustryGroup = matches[2].Groups[2].Value; } } stocksContext.SaveChanges(); }
public async Task Update(StocksContext stocksContext) { var endOfDays = await GetEndOfDays(stocksContext); DeleteExistingEndOfDaysForSameDates(endOfDays, stocksContext); stocksContext.EndOfDay.AddRange(endOfDays); stocksContext.SaveChanges(); }
private void DeleteExistingEndOfDaysForSameDates(IList <EndOfDay> endOfDays, StocksContext stocksContext) { var endOfDayDates = endOfDays .Select(e => e.Date) .Distinct() .ToList(); var endOfDaysToDelete = stocksContext.EndOfDay .Where(e => endOfDayDates.Contains(e.Date)) .ToList(); stocksContext.EndOfDay.RemoveRange(endOfDaysToDelete); stocksContext.SaveChanges(); }
public virtual int Save() { int returnValue = 200; using (var dbContextTransaction = Context.Database.BeginTransaction()) // { try { Context.SaveChanges(); dbContextTransaction.Commit(); } catch (DbUpdateException ex) { var sqlException = ex.GetBaseException() as SqlException; if (sqlException != null) { var number = sqlException.Number; if (number == 547) { returnValue = 501; } else { returnValue = 500; } } } catch (Exception ex) { //Log Exception Handling message returnValue = 500; dbContextTransaction.Rollback(); } // } return(returnValue); }