Exemple #1
0
        protected override void Seed(DatabaseBudStorage db)
        {
            Dictionary <int, string> units_in_db = new Dictionary <int, string>();

            // UnitsOfMeasurement
            string filePathForUnitsOfMeasurement = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\StartDB\Units_of_measurement.xlsx";
            Excel  tableUnitsOfMeasurement       = new Excel(filePathForUnitsOfMeasurement, 1);

            for (int i = 2; i <= Int32.Parse(tableUnitsOfMeasurement.ReadCell(1, 1)); ++i)
            {
                Unit_Of_Measurement unit = new Unit_Of_Measurement()
                {
                    Code    = Int32.Parse(tableUnitsOfMeasurement.ReadCell(i, 1)),
                    Name    = tableUnitsOfMeasurement.ReadCell(i, 2),
                    Marking = tableUnitsOfMeasurement.ReadCell(i, 3)
                };

                units_in_db.Add(i - 1, unit.Marking);

                db.Entities_Unit_Of_Measurements.Add(unit);
            }
            tableUnitsOfMeasurement.CloseFile();

            //Products
            string filePathForProducts = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\StartDB\Products.xlsx";
            Excel  tableProducts       = new Excel(filePathForProducts, 1);

            for (int i = 2; i <= Int32.Parse(tableProducts.ReadCell(1, 1)); ++i)
            {
                Product prod = new Product()
                {
                    Code = Int32.Parse(tableProducts.ReadCell(i, 1)),
                    Name = tableProducts.ReadCell(i, 2),
                    King = tableProducts.ReadCell(i, 3),
                };

                foreach (var unt in units_in_db)
                {
                    if (tableProducts.ReadCell(i, 4) == unt.Value)
                    {
                        prod.IdUnit_Of_Measurement = unt.Key;
                    }
                }

                db.Entities_Products.Add(prod);
            }
            tableProducts.CloseFile();

            //Contractors
            string filePathForContractors = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\StartDB\Contractors.xlsx";
            Excel  tableContractors       = new Excel(filePathForContractors, 1);

            for (int i = 2; i <= Int32.Parse(tableContractors.ReadCell(1, 1)); ++i)
            {
                Contractor contract = new Contractor()
                {
                    Short_Name = tableContractors.ReadCell(i, 1),
                    Full_Name  = tableContractors.ReadCell(i, 2),
                    EDRPOU     = tableContractors.ReadCell(i, 3),
                };

                db.Entities_Contractors.Add(contract);
            }
            tableContractors.CloseFile();

            //Warehouses
            string filePathForWarehouses = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\StartDB\Warehouses.xlsx";
            Excel  tableWarehouses       = new Excel(filePathForWarehouses, 1);

            for (int i = 2; i <= Int32.Parse(tableWarehouses.ReadCell(1, 1)); ++i)
            {
                Warehouse warehouse = new Warehouse()
                {
                    Name = tableWarehouses.ReadCell(i, 1),
                };

                db.Entities_Warehouses.Add(warehouse);
            }
            tableWarehouses.CloseFile();

            db.SaveChanges();
        }