예제 #1
0
        /// <summary>
        /// 通过传入的model加需要修改的字段来更改数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="propertys"></param>
        public void Edit(TEntity model, string[] propertys)
        {
            if (model == null)
            {
                throw new Exception("实体不能为空");
            }

            if (propertys.Any() == false)
            {
                throw new Exception("要修改的属性至少要有一个");
            }

            //将model追击到EF容器
            DbEntityEntry entry = db.Entry(model);

            entry.State = EntityState.Unchanged;

            foreach (var item in propertys)
            {
                entry.Property(item).IsModified = true;
            }

            //关闭EF对于实体的合法性验证参数
            db.Configuration.ValidateOnSaveEnabled = false;
        }
예제 #2
0
        private static void SendBackMsg()
        {
            //   huobiEntities hbdb = new huobiEntities();

            DateTime time1 = new DateTime();

            List <hb_symbols> symbols = null;

            int count = 150;

            Log.error("测试日志");

            while (true)
            {
                //半小时取一次所有交易对

                try
                {
                    DateTime time2 = DateTime.Now;

                    if ((time2 - time1).TotalMinutes > 30)
                    {
                        symbols = server.getAllSymbol();
                        time1   = time2;
                    }

                    if (symbols != null)
                    {
                        //去取数据
                        using (huobiEntities hbdb = new huobiEntities())
                        {
                            foreach (var item in symbols)
                            {
                                if (item.symbol_partition == "main" || item.symbol_partition == "innovation")
                                {
                                    string symbol = item.base_currency + item.quote_currency;

                                    List <hb_kline> data = server.getKLine(symbol, "1day", count);

                                    //存数据库....
                                    DateTime time3 = DateTime.Now;

                                    DateTime time4 = time3.Date.AddHours(23).AddMinutes(59);

                                    if (data != null && data.Count > 0)
                                    {
                                        for (var i = 0; i < data.Count; i++)
                                        {
                                            hb_kline linedata = data[i];

                                            DateTime temptime  = time4.AddDays(-i).Date;
                                            DateTime temptime2 = temptime.Date.AddDays(1);

                                            market m = hbdb.market.FirstOrDefault(a => (a.symbols == symbol && a.last_time >= temptime && a.last_time < temptime2));

                                            if (m == null)
                                            {
                                                m = new market();

                                                m.symbols     = symbol;
                                                m.coin_type   = item.base_currency;
                                                m.token_type  = item.quote_currency;
                                                m.open_price  = linedata.open;
                                                m.close_price = linedata.close;
                                                m.rose        = m.open_price != 0 ? (m.close_price - m.open_price) / m.open_price : 0;
                                                m.turnover    = 0;
                                                m.last_time   = i == 0 ? time3 : time4.AddDays(-i);

                                                hbdb.market.Add(m);
                                            }
                                            else
                                            {
                                                m.open_price  = linedata.open;
                                                m.close_price = linedata.close;
                                                m.rose        = m.open_price != 0 ? (m.close_price - m.open_price) / m.open_price : 0;
                                                m.turnover    = 0;
                                                m.last_time   = i == 0 ? time3 : time4.AddDays(-i);

                                                hbdb.Entry(m).State = EntityState.Modified;
                                            }
                                        }

                                        hbdb.SaveChanges();

                                        Thread.Sleep(100);
                                    }
                                }
                            }
                        }
                        count = 1;
                    }
                }
                catch (Exception ex)
                {
                    Log.error(ex);
                }

                Thread.Sleep(5000);
            }
        }