Пример #1
0
        public void Execute()
        {
            string key    = "image.existchek.lastid";
            int    fromID = 0;

            if (LS.IsExistInCache(key))
            {
                fromID = LS.GetFromCache <int>(key);
            }

            using (Db _db = new Db())
            {
                var products = _db.Products.Where(x => x.HasImage && x.ID > fromID)
                               .Select(x => new
                {
                    x.ID,
                    x.Image
                }).Take(1000).ToList();
                LS.SetToCache(products.Select(x => x.ID).DefaultIfEmpty(0).Max(), key);
                foreach (var p in products)
                {
                    if (!string.IsNullOrEmpty(p.Image))
                    {
                        var path = HostingEnvironment.MapPath("~" + p.Image);
                        if (path.Contains("wwwroot"))
                        {
                            if (!System.IO.File.Exists(path))
                            {
                                var sql = string.Format("UPDATE [{0}] SET [Image] = null, HasImage = 0  WHERE [ID] = {1} ",
                                                        "Products",
                                                        p.ID //,
                                                             // p.Image.Replace("'", @"''") //fix string insert
                                                        );
                                _db.Database.ExecuteSqlCommand(sql);
                            }
                        }
                    }
                    else
                    {
                        var sql = string.Format("UPDATE [{0}] SET [Image] = null, HasImage = 0  WHERE [ID] = {1} ",
                                                "Products",
                                                p.ID //,
                                                     // p.Image.Replace("'", @"''") //fix string insert
                                                );
                        _db.Database.ExecuteSqlCommand(sql);
                    }
                }
            }
        }
        public void Execute()
        {
            lock (_lock)
            {
                string key = "image.importsync.lock";

                if (LS.IsExistInCache(key) && LS.GetFromCache <bool>(key))
                {
                    return; // already running
                }
                LS.SetToCache(true, key);
                using (Db _db = new Db())
                {
                }

                //laset process
                LS.SetToCache(false, key);
            }
        }