//public bool LoadPriceBarData(Security security) //{ // using (var db = new PriceDatabaseContext()) // { // if (db.Entry(security).Collection(s => s.DailyPriceBarData) != null) // { // var ent = db.Entry(security); // ent.State = EntityState.Unchanged; // ent.Collection(x => x.DailyPriceBarData).Load(); // return true; // } // return false; // } //} public Security GetSecurity(string ticker, bool create = true, bool track = false) { using (var db = new PriceDatabaseContext(ConnectionString)) { ticker = ticker.Trim(); var ret = (from sec in (track ? db.Securities .Include(x => x.DailyPriceBarData) : db.Securities.AsNoTracking() .Include(x => x.DailyPriceBarData)) where sec.Ticker == ticker select sec).SingleOrDefault(); if (ret != null || !create) { return ret; } ret = db.Securities.AddAndReturn(new Security(ticker)); db.SaveChanges(); return ret; } }
public void RemoveSecurity(Security security) { using (var db = new PriceDatabaseContext(ConnectionString)) { try { //var sec = GetSecurity(security.Ticker, false, true); //db.Securities.Remove(sec); //db.SaveChanges(); db.Securities.Attach(security); db.Entry(security).State = EntityState.Deleted; db.SaveChanges(); } catch (Exception ex) { Log(new LogMessage(ToString() + ".RemoveSecurity()", ex.Message, LogMessageType.SystemError)); //throw ex; } } }
public bool SetSecurity(Security security, bool Overwrite) { if (security.DailyPriceBarData.Any(x => x.BarSize != PriceBarSize.Daily)) throw new UnknownErrorException() { message = "Price Bar data invalid" }; try { using (var db = new PriceDatabaseContext(ConnectionString)) { //// Get the Security entity stored in the database var dbSecurity = (from sec in db.Securities where sec.Ticker == security.Ticker select sec).Include(x => x.DailyPriceBarData).FirstOrDefault(); if (dbSecurity == null) { throw new SecurityNotFoundException() { message = "Attempted to reattach non-existent security to database" }; } db.Entry(dbSecurity).CurrentValues.SetValues(security); foreach (var newBar in security.DailyPriceBarData) { try { // Skip bars not marked for update if (!newBar.ToUpdate && !Overwrite) continue; // Find existing bar if one exists for this date var currentBar = dbSecurity.DailyPriceBarData.Where(currBar => currBar == newBar); if (currentBar?.Count() > 1) { // More than one bar got saved... delete both and save the new one currentBar.ToList().ForEach(b => db.Entry(b).State = EntityState.Deleted); //// Insert new bar newBar.Security = dbSecurity; dbSecurity.DailyPriceBarData.Add(newBar); newBar.ToUpdate = false; db.SaveChanges(); } else if (currentBar.SingleOrDefault() != null) { if (!Overwrite) continue; // Update bars that are in the new bar collection db.Entry(currentBar).CurrentValues.SetValues(newBar); newBar.ToUpdate = false; } else { //// Insert new bars newBar.Security = dbSecurity; newBar.ToUpdate = false; dbSecurity.DailyPriceBarData.Add(newBar); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } db.SaveChanges(); return true; } } catch (Exception ex) { Log(new LogMessage(ToString() + ".SetSecurity()", $"Could not set data for {security.Ticker}; {ex.Message}", LogMessageType.SystemError)); return false; } }