public virtual void Delete(T data) { using (StockManDBEntities db = new StockManDBEntities()) { Type type = data.GetType(); PropertyInfo[] members = type.GetProperties(); List <string> ids = new List <string>(); foreach (PropertyInfo m in members) { var temp = m.GetCustomAttribute <KeyAttribute>(); if (temp != null) { object obj = m.GetValue(data); ids.Add(obj.ToString()); continue; } } T entity = db.Set <T>().Find(ids.ToArray()); if (entity != default(T)) { db.Set <T>().Remove(entity); db.SaveChanges(); } } }
public virtual void DeleteRange(IList <T> datas) { using (StockManDBEntities db = new StockManDBEntities()) { IList <T> list = new List <T>(); foreach (T data in datas) { Type type = data.GetType(); PropertyInfo[] members = type.GetProperties(); List <string> ids = new List <string>(); foreach (PropertyInfo m in members) { var temp = m.GetCustomAttribute <KeyAttribute>(); if (temp != null) { object obj = m.GetValue(data); ids.Add(obj.ToString()); continue; } } T entity = db.Set <T>().Find(ids.ToArray()); list.Add(entity); } if (list.Count > 0) { foreach (var l in list) { db.Set <T>().Remove(l); } db.SaveChanges(); } } }
public virtual IList <T> FindAll() { using (StockManDBEntities entity = new StockManDBEntities()) { return(entity.Set <T>().ToList()); } }
public virtual T Find(string id) { using (StockManDBEntities db = new StockManDBEntities()) { T entity = db.Set <T>().Find(id); return(entity); } }
public virtual T Add(T data) { using (StockManDBEntities entity = new StockManDBEntities()) { entity.Set <T>().Add(data); try { entity.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { throw ex; } return(data); } }
public virtual void AddRange(IList <T> datas) { using (StockManDBEntities entity = new StockManDBEntities()) { foreach (var data in datas) { entity.Set <T>().Add(data); } try { entity.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException ex) { throw ex; } } }
public void AddPriceByMonth <TM>(IList <PriceInfo> price, bool check = true) where TM : ObjectDataBase, new() { using (StockManDBEntities entity = new StockManDBEntities()) { string tableName = typeof(TM).Name; DateTime nextMonthData = DateTime.Now.AddMonths(1); DateTime endDate = new DateTime(nextMonthData.Year, nextMonthData.Month, 1); DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1); //IList<TM> list = new List<TM>(); foreach (PriceInfo p in price) { if (!p.open.HasValue || p.open.Value <= 0) { continue; } string startCode = p.code + "_" + startDate.ToString("yyyyMMdd"); string endCode = p.code + "_" + endDate.ToString("yyyyMMdd"); string code = p.code + "_" + p.date.ToString("yyyyMMdd"); //判断是否有记录,有的话,不插入 if (check) { if (entity.Set <TM>().Count(s => s.code == code) > 0) { continue; } //从日线里寻找开盘价,收盘价,最高价,最低价 decimal?open, close, high, low, updown, volume, turnover, percent = 0, yestclose; string statisSql = string.Format("select code,date,open,price,yestclose,high,low,percent,updown,volume,turnover from {0} a where a.code between @p0 and @p1", tableName.Replace("month", "week")); var racentPrice = entity.Database.SqlQuery <PriceInfo>(statisSql, startCode, endCode).OrderBy(pi => pi.date).ToList(); if (racentPrice.Count == 0) { continue; } if (racentPrice.Count > 1) { yestclose = racentPrice.First().yestclose; open = racentPrice.First().open; close = racentPrice.Last().price; high = racentPrice.Max(pp => pp.high); low = racentPrice.Max(pp => pp.low); updown = close - open; volume = racentPrice.Sum(pp => pp.volume); turnover = racentPrice.Sum(pp => pp.turnover); if (yestclose != 0) { percent = (close - yestclose) / yestclose; } else { percent = 0; } } else { open = racentPrice[0].open; close = racentPrice[0].price; high = racentPrice[0].high; low = racentPrice[0].low; updown = racentPrice[0].updown; volume = racentPrice[0].volume; turnover = racentPrice[0].turnover; percent = racentPrice[0].percent; yestclose = racentPrice[0].yestclose; } var countRow = entity.Database.SqlQuery <int>(string.Format("select count(0) from {0} a where a.code between @p0 and @p1", tableName), startCode, endCode); if (countRow.First() > 0) { entity.Database.ExecuteSqlCommand(string.Format("delete from {0} where code between @p0 and @p1", tableName), startCode, endCode); } entity.Set <TM>().Add(new TM() { code = code, date = p.date,// new DateTime(int.Parse(date.Substring(0, 4)), int.Parse(date.Substring(4, 2)), int.Parse(p.date.Substring(6))), price = close, yestclose = yestclose, high = high, low = low, open = open, percent = percent, object_code = p.code, updown = updown, volume = volume, turnover = turnover }); } else { entity.Set <TM>().Add(new TM() { code = code, date = p.date, price = p.price, yestclose = p.yestclose, high = p.high, low = p.low, open = p.open, percent = p.percent, object_code = p.code, updown = p.updown, volume = p.volume, turnover = p.turnover }); } } //foreach (var l in list) //{ // entity.Set<TM>().Add(l); //} try { entity.SaveChanges(); } catch (Exception ex) { throw ex; } } }
public void AddPriceByDay <TM>(IList <PriceInfo> priceList, bool check = true) where TM : ObjectDataBase, new() { using (StockManDBEntities entity = new StockManDBEntities()) { string tableName = typeof(TM).Name; //IList<TM> list = new List<TM>(); foreach (PriceInfo p in priceList) { if (!p.open.HasValue || p.open.Value <= 0) { continue; } string code = p.code + "_" + p.date.ToString("yyyyMMdd"); if (check && entity.Set <TM>().Count(s => s.code == code) > 0) { entity.Entry(new TM() { code = code, date = p.date,//new DateTime(int.Parse(date.Substring(0, 4)), int.Parse(date.Substring(4, 2)), int.Parse(p.date.Substring(6))), price = p.price, yestclose = p.yestclose, high = p.high, low = p.low, open = p.open, percent = p.percent, object_code = p.code, updown = p.updown, volume = p.volume, turnover = p.turnover }).State = EntityState.Modified; } else { entity.Set <TM>().Add(new TM() { code = code, date = p.date,//new DateTime(int.Parse(date.Substring(0, 4)), int.Parse(date.Substring(4, 2)), int.Parse(p.date.Substring(6))), price = p.price, yestclose = p.yestclose, high = p.high, low = p.low, open = p.open, percent = p.percent, object_code = p.code, updown = p.updown, volume = p.volume, turnover = p.turnover }); } } //foreach (var l in list) //{ // entity.Set<TM>().Add(l); //} try { entity.SaveChanges(); } catch (Exception ex) { throw ex; } } }