private static void AddSaleToDatabase(
            int rowsCount,
            DataRowCollection rows,
            MSSQLContext context,
            Supermarket supermarket,
            DateTime saleDate)
        {
            const int startRow = 3;

            for (int i = startRow; i < rowsCount - 1; i++)
            {
                string productName = rows[i][1].ToString();
                int quantity = int.Parse(rows[i][2].ToString());
                decimal price = decimal.Parse(rows[i][3].ToString());

                Product product = context.Products.FirstOrDefault(p => p.Name == productName);

                var sale = new Sale()
                {
                    Supermarket = supermarket,
                    Product = product,
                    SaleDate = saleDate,
                    SalePrice = price,
                    Quantity = quantity
                };

                context.Sales.Add(sale);
                context.SaveChanges();
            }
        }
Beispiel #2
0
    static void AddSupermarket(string name)
    {
        using (var db = new SupermarketDB())
        {
            var count = db.Supermarkets.Where(x => x.StoreName == name).Count();

            if (count == 0)
            {
                var supermarket = new Supermarket.Models.Supermarket {
                    StoreName = name
                };
                db.Supermarkets.Add(supermarket);
                db.SaveChanges();
            }
        }
    }
        private static Supermarket GetSupermarket(MSSQLContext context, string supermarketName)
        {
            var supermarket = context.Supermarkets.FirstOrDefault(s => s.Name == supermarketName);
            if (supermarket == null)
            {
                supermarket = new Supermarket()
                {
                    Name = supermarketName
                };

                context.Supermarkets.Add(supermarket);
            }

            return supermarket;
        }
        private static void ReplicateSupermarket(MySQLContext context, IQueryable<Supermarket> supermarkets)
        {
            foreach (var supermarket in supermarkets)
            {
                if (!context.Supermarkets.Any(s => s.Name == supermarket.Name))
                {
                    var newSupermarket = new Supermarket()
                    {
                        Name = supermarket.Name
                    };

                    context.Supermarkets.Add(newSupermarket);

                    context.SaveChanges();
                }
            }
        }