Exemplo n.º 1
0
        public static void MoveDatabase()
        {
            using (OldEntitiesModel context = new OldEntitiesModel())
            {
                using (NewEntitiesModel newContext = new NewEntitiesModel())
                {
                    foreach (var product in context.Products)
                    {
                        SqlServer.Product newProduct = new SqlServer.Product();
                        newProduct.Id = product.Id;
                        newProduct.BasePrice = product.BasePrice;
                        newProduct.ProductName = product.ProductName;
                        newContext.Products.Add(newProduct);
                        newContext.SaveChanges();
                    }

                    foreach (var vendor in context.Vendors)
                    {
                        SqlServer.Vendor newVendor = new SqlServer.Vendor();
                        newVendor.Id = vendor.Id;
                        newVendor.VendorName = vendor.VendorName;
                        foreach (var product in vendor.Products)
                        {
                            var productToAdd = newContext.Products.Where(
                                p => p.Id.Equals(product.Id))
                                .FirstOrDefault();
                            if (productToAdd != null)
                            {
                                newVendor.Products.Add(productToAdd);
                            }
                        }

                        newContext.Vendors.Add(newVendor);
                        newContext.SaveChanges();
                    }

                    foreach (var measure in context.Measures)
                    {
                        SqlServer.Measure newMeasure = new SqlServer.Measure();
                        newMeasure.Id = measure.Id;
                        newMeasure.MeasureName = measure.MeasureName;
                        foreach (var product in measure.Products)
                        {
                            var productToAdd = newContext.Products.Where(
                                p => p.Id.Equals(product.Id))
                                .FirstOrDefault();
                            if (productToAdd != null)
                            {
                                newMeasure.Products.Add(productToAdd);
                            }
                        }

                        newContext.Measures.Add(newMeasure);
                        newContext.SaveChanges();
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void ParseIntoSqlServer(VendorExpensesEntity entity, NewEntitiesModel context)
        {
            MonthlyExpenseReport report = new MonthlyExpenseReport();
            report.Date = entity.Date;
            report.Expenses = entity.Expenses;

            int reportId = -1;
            if (context.MonthlyExpenseReports.Count() == 0)
            {
                reportId = 0;
            }
            else
            {
                reportId = context.MonthlyExpenseReports.Max(r => r.Id);
            }

            reportId++;
            report.Id = reportId;

            var vendor = context.Vendors.Where(
                                s => s.VendorName.Equals(entity.VendorName))
                                .FirstOrDefault();

            if (vendor == null)
            {
                vendor = new Vendor();
                vendor.VendorName = entity.VendorName;

                int biggestId = -1;
                if (context.Vendors.Count() == 0)
                {
                    biggestId = 0;
                }
                else
                {
                    biggestId = context.Vendors.Max(i => i.Id);
                }

                biggestId++;
                vendor.Id = biggestId;
                context.Vendors.Add(vendor);
                context.SaveChanges();
            }

            report.VendorId = vendor.Id;
            context.MonthlyExpenseReports.Add(report);
            context.SaveChanges();
        }
Exemplo n.º 3
0
        private static void WriteToDatabase(string supermarketName, string[] currentArguments, NewEntitiesModel context, DateTime date)
        {
            Report generatedReport = new Report();

            generatedReport.ProductId = int.Parse(currentArguments[0]);
            generatedReport.Quantity = decimal.Parse(currentArguments[1]);
            generatedReport.UnitPrice = decimal.Parse(currentArguments[2]);
            generatedReport.Sum = decimal.Parse(currentArguments[3]);
            generatedReport.Date = date;

            int reportId = -1;
            if (context.Reports.Count() == 0)
            {
                reportId = 0;
            }
            else
            {
                reportId = context.Reports.Max(r => r.Id);
            }

            reportId++;

            generatedReport.Id = reportId;

            var supermarket = context.Supermarkets.Where(
                                s => s.SupermarketName.Equals(supermarketName))
                                .FirstOrDefault();

            if (supermarket == null)
            {
                supermarket = new Supermarket();
                supermarket.SupermarketName = supermarketName;

                int biggestId = -1;
                if (context.Supermarkets.Count() == 0)
                {
                    biggestId = 0;
                }
                else
                {
                    biggestId = context.Supermarkets.Max(i => i.Id);
                }

                biggestId++;
                supermarket.Id = biggestId;
                context.Supermarkets.Add(supermarket);
                context.SaveChanges();
            }

            generatedReport.SupermarketId = supermarket.Id;
            context.Reports.Add(generatedReport);
            context.SaveChanges();
        }