예제 #1
0
        private static void SeedCats(EFDataContext context)
        {
            if (!context.Cats.Any())
            {
                AppCat cat = new AppCat {
                    Name     = "Манул",
                    Details  = "Деталі про манула",
                    Birthday = DateTime.Now,
                    ImgUrl   = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Manoel.jpg/275px-Manoel.jpg",
                };
                context.Cats.Add(cat);
                context.SaveChanges();

                var         firstCat = context.Cats.FirstOrDefault();
                AppCatPrice price    = new AppCatPrice
                {
                    CatId      = firstCat.Id,
                    DateCreate = DateTime.Now,
                    Price      = 500
                };
                context.CatPrices.Add(price);

                context.SaveChanges();
            }
        }
예제 #2
0
        private void AddCats()
        {
            ParallelOptions parOpt = new ParallelOptions();

            parOpt.MaxDegreeOfParallelism = System.Environment.ProcessorCount;
            parOpt.CancellationToken      = tokenSource.Token;
            Parallel.Invoke(parOpt, new Action(() => {
                for (int i = 1; i <= _result; i++)
                {
                    try
                    {
                        parOpt.CancellationToken.ThrowIfCancellationRequested();
                        Dispatcher.Invoke(new Action(() =>
                        {
                            this.catProgres.Value = i;
                        }));
                        Thread.Sleep(300);
                        CatModel model = faker.Generate();
                        AppCat cat     = new AppCat {
                            Name     = model.Name,
                            ImgUrl   = model.ImgUrl,
                            Details  = model.Details,
                            Birthday = model.Birthday,
                        };
                        _context.Cats.Add(cat);
                        _context.SaveChanges();

                        AppCatPrice price = new AppCatPrice {
                            DateCreate = DateTime.Now,
                            CatId      = cat.Id,
                            Price      = model.Price
                        };
                        _context.CatPrices.Add(price);
                        _context.SaveChanges();
                    }
                    catch
                    {
                        return;
                    }
                }
            }));
        }