private void CopyEmployeeTerritories(IOdb odb)
        {
            //Processing EmployeeTerritories
            LogMessage("Reading EmployeeTerritories...", false);
            var adapter1 = new EmployeeTerritoriesTableAdapter();
            var table1   = adapter1.GetData();

            LogMessage("processing " + table1.Count.ToString() + " rows", true);
            foreach (var row in table1)
            {
                LogMessage("EmployeeTerritories: " + row.EmployeeID.ToString() + "/" + row.TerritoryID + " ...", false);

                var et = new EmployeeTerritory();
                LogMessage("linking members...", false);
                et.Employee  = NDbUtil.GetByNumericalID <Employee>(odb, Employee.PK, row.EmployeeID);
                et.Territory = NDbUtil.GetByStringID <Territory>(odb, Territory.PK, row.TerritoryID);

                odb.Store(et);
                LogMessage("saved (" + et.Employee.EmployeeID.ToString() + "/" + et.Territory.TerritoryID + ")",
                           true);
            }
            odb.Commit();

            long objectCount = NDbUtil.GetAllInstances <EmployeeTerritory>(odb).Count;

            if (table1.Count == objectCount)
            {
                LogMessage(table1.Count + " objects saved", true);
            }
            else
            {
                LogMessage("Error: " + table1.Count + " rows retrieved but " + objectCount + " objects were saved", true);
            }
            LogMessage("Done with EmployeeTerritories" + Environment.NewLine, true);
        }
        private void CopyTerritories(IOdb odb)
        {
            //Processing Territories
            LogMessage("Reading Territories...", false);
            var adapter1 = new TerritoriesTableAdapter();
            var table1   = adapter1.GetData();

            LogMessage("processing " + table1.Count.ToString() + " rows", true);
            foreach (var row in table1)
            {
                LogMessage("Territories: " + row.TerritoryID + " ...", false);

                var t = new Territory {
                    TerritoryID = row.TerritoryID, TerritoryDescription = row.TerritoryDescription
                };

                LogMessage("linking member...", false);
                t.Region = NDbUtil.GetByNumericalID <Region>(odb, Domain.Region.PK, row.RegionID);

                odb.Store(t);
                LogMessage("saved (" + t.TerritoryID + ")", true);
            }
            odb.Commit();

            LogMessage("Commit done, starting create index ...", false);
            odb.IndexManagerFor <Territory>().AddUniqueIndexOn("Territory_TerritoryID_PK_index", Territory.PK);
            odb.Commit();
            LogMessage(" index created.", true);

            long objectCount = NDbUtil.GetAllInstances <Territory>(odb).Count;

            if (table1.Count == objectCount)
            {
                LogMessage(table1.Count + " objects saved", true);
            }
            else
            {
                LogMessage("Error: " + table1.Count + " rows retrieved but " + objectCount + " objects were saved", true);
            }
            LogMessage("Done with Territories" + Environment.NewLine, true);
        }
        private void CopyOrderDetails(IOdb odb, ref double progress)
        {
            //Processing OrderDetails
            LogMessage("Reading OrderDetails...", false);
            var adapter1 = new Order_DetailsTableAdapter();
            var table1   = adapter1.GetData();

            LogMessage("processing " + table1.Count.ToString() + " rows", true);
            foreach (var row in table1)
            {
                LogMessage("OrderDetails: " + row.OrderID.ToString() + "/" + row.ProductID.ToString() + " ...", false);

                var od = new OrderDetail();
                LogMessage("linking members...", false);
                od.Order     = NDbUtil.GetByNumericalID <Order>(odb, Order.PK, row.OrderID);
                od.Product   = NDbUtil.GetByNumericalID <Product>(odb, Product.PK, row.ProductID);
                od.UnitPrice = Convert.ToDouble(row.UnitPrice);
                od.Quantity  = row.Quantity;
                od.Discount  = Convert.ToDouble(row.Discount);

                odb.Store(od);
                StepFinished(progress += 0.029);
                LogMessage("saved (" + od.Order.OrderID.ToString() + "/" + od.Product.ProductID.ToString() + ")",
                           true);
            }
            odb.Commit();

            long objectCount = NDbUtil.GetAllInstances <OrderDetail>(odb).Count;

            if (table1.Count == objectCount)
            {
                LogMessage(table1.Count + " objects saved", true);
            }
            else
            {
                LogMessage("Error: " + table1.Count + " rows retrieved but " + objectCount + " objects were saved", true);
            }
            LogMessage("Done with OrderDetails" + Environment.NewLine, true);
        }
        private void CopyOrders(IOdb odb, ref double progress)
        {
            var orders    = new List <Order>();
            var customers = new Hashtable();
            var employees = new Hashtable();
            var shippers  = new Hashtable();

            //Processing Orders
            LogMessage("Reading Orders...", false);
            var adapter1 = new OrdersTableAdapter();
            var table1   = adapter1.GetData();

            LogMessage("processing " + table1.Count.ToString() + " rows", true);
            foreach (var row in table1)
            {
                var o = new Order
                {
                    OrderID        = row.OrderID,
                    Freight        = row.IsFreightNull() ? 0 : Convert.ToDouble(row.Freight),
                    ShipName       = row.IsShipNameNull() ? null : row.ShipName,
                    ShipAddress    = row.IsShipAddressNull() ? null : row.ShipAddress,
                    ShipCity       = row.IsShipCityNull() ? null : row.ShipCity,
                    ShipRegion     = row.IsShipRegionNull() ? null : row.ShipRegion,
                    ShipPostalCode = row.IsShipPostalCodeNull() ? null : row.ShipPostalCode,
                    ShipCountry    = row.IsShipCountryNull() ? null : row.ShipCountry
                };

                if (!row.IsCustomerIDNull())
                {
                    customers.Add(o.OrderID, row.CustomerID);
                }
                if (!row.IsEmployeeIDNull())
                {
                    employees.Add(o.OrderID, row.EmployeeID);
                }
                if (!row.IsShipViaNull())
                {
                    shippers.Add(o.OrderID, row.ShipVia);
                }
                if (!row.IsOrderDateNull())
                {
                    o.OrderDate = row.OrderDate;
                }
                if (!row.IsRequiredDateNull())
                {
                    o.RequiredDate = row.RequiredDate;
                }
                if (!row.IsShippedDateNull())
                {
                    o.ShippedDate = row.ShippedDate;
                }

                orders.Add(o);
            }
            foreach (var o in orders)
            {
                LogMessage("Order: " + o.OrderID.ToString() + " ...", false);
                if (customers.ContainsKey(o.OrderID))
                {
                    LogMessage("linking member...", false);
                    var customerID = Convert.ToString(customers[o.OrderID]);
                    var found      = NDbUtil.GetByStringID <Customer>(odb, Customer.PK, customerID);
                    o.Customer = found;
                }
                if (employees.ContainsKey(o.OrderID))
                {
                    LogMessage("linking member...", false);
                    var employeeID = Convert.ToInt64(employees[o.OrderID]);
                    var found      = NDbUtil.GetByNumericalID <Employee>(odb, Employee.PK, employeeID);
                    o.Employee = found;
                }
                if (shippers.ContainsKey(o.OrderID))
                {
                    LogMessage("linking member...", false);
                    var shipperID = Convert.ToInt64(shippers[o.OrderID]);
                    var found     = NDbUtil.GetByNumericalID <Shipper>(odb, Shipper.PK, shipperID);
                    o.ShipVia = found;
                }
                odb.Store(o);
                StepFinished(progress += 0.029);
                LogMessage("saved (" + o.OrderID.ToString() + ")", true);
            }
            odb.Commit();

            LogMessage("Commit done, starting create index ...", false);
            odb.IndexManagerFor <Order>().AddUniqueIndexOn("Order_OrderID_PK_index", Order.PK);
            odb.Commit();
            LogMessage(" index created.", true);

            long objectCount = NDbUtil.GetAllInstances <Order>(odb).Count;

            if (table1.Count == objectCount)
            {
                LogMessage(table1.Count + " objects saved", true);
            }
            else
            {
                LogMessage("Error: " + table1.Count + " rows retrieved but " + objectCount + " objects were saved", true);
            }
            LogMessage("Done with Orders" + Environment.NewLine, true);
        }
        private void CopyProducts(IOdb odb)
        {
            var products   = new List <Product>();
            var suppliers  = new Hashtable();
            var categories = new Hashtable();

            //Processing Products
            LogMessage("Reading Products...", false);
            var adapter1 = new ProductsTableAdapter();
            var table1   = adapter1.GetData();

            LogMessage("processing " + table1.Count.ToString() + " rows", true);
            foreach (var row in table1)
            {
                var p = new Product
                {
                    ProductID       = row.ProductID,
                    ProductName     = row.ProductName,
                    QuantityPerUnit = row.IsQuantityPerUnitNull() ? null : row.QuantityPerUnit,
                    UnitPrice       = row.IsUnitPriceNull() ? 0 : Convert.ToDouble(row.UnitPrice),
                    UnitsInStock    = row.IsUnitsInStockNull() ? 0 : row.UnitsInStock,
                    UnitsOnOrder    = row.IsUnitsOnOrderNull() ? 0 : row.UnitsOnOrder,
                    ReorderLevel    = row.IsReorderLevelNull() ? 0 : row.ReorderLevel,
                    Discontinued    = row.Discontinued
                };

                if (!row.IsSupplierIDNull())
                {
                    suppliers.Add(p.ProductID, row.SupplierID);
                }
                if (!row.IsCategoryIDNull())
                {
                    categories.Add(p.ProductID, row.CategoryID);
                }

                products.Add(p);
            }
            foreach (var p in products)
            {
                LogMessage("Product: " + p.ProductID.ToString() + " ...", false);
                if (suppliers.ContainsKey(p.ProductID))
                {
                    LogMessage("linking member...", false);
                    var supplierID = Convert.ToInt64(suppliers[p.ProductID]);
                    var found      = NDbUtil.GetByNumericalID <Supplier>(odb, Supplier.PK, supplierID);
                    p.Supplier = found;
                }
                if (categories.ContainsKey(p.ProductID))
                {
                    LogMessage("linking member...", false);
                    var categoryID = Convert.ToInt64(categories[p.ProductID]);
                    var found      = NDbUtil.GetByNumericalID <Category>(odb, Category.PK, categoryID);
                    p.Category = found;
                }
                odb.Store(p);

                LogMessage("saved (" + p.ProductID.ToString() + ")", true);
            }
            odb.Commit();

            LogMessage("Commit done, starting create index ...", false);
            odb.IndexManagerFor <Product>().AddUniqueIndexOn("Product_ProductID_PK_index", Product.PK);
            odb.Commit();
            LogMessage(" index created.", true);

            long objectCount = NDbUtil.GetAllInstances <Product>(odb).Count;

            if (table1.Count == objectCount)
            {
                LogMessage(table1.Count + " objects saved", true);
            }
            else
            {
                LogMessage("Error: " + table1.Count + " rows retrieved but " + objectCount + " objects were saved", true);
            }
            LogMessage("Done with Products" + Environment.NewLine, true);
        }