Esempio n. 1
0
        public void AddProducts(NorthwindDataConnection connection, List <Product> products)
        {
            try
            {
                connection.BeginTransaction();
                foreach (var product in products)
                {
                    var category = connection.Categories.FirstOrDefault(c => c.CategoryName == product.Category.CategoryName);
                    product.CategoryID = category?.CategoryID ?? Convert.ToInt32(connection.InsertWithIdentity(
                                                                                     new Category
                    {
                        CategoryName = product.Category.CategoryName
                    }));
                    var supplier = connection.Suppliers.FirstOrDefault(s => s.CompanyName == product.Supplier.CompanyName);
                    product.SupplierID = supplier?.SupplierID ?? Convert.ToInt32(connection.InsertWithIdentity(
                                                                                     new Supplier
                    {
                        CompanyName = product.Supplier.CompanyName
                    }));
                }

                connection.BulkCopy(products);
                connection.CommitTransaction();
            }
            catch
            {
                connection.RollbackTransaction();
            }
        }
Esempio n. 2
0
        public void AddNewEpmloyeeWithTerretory(NorthwindDataConnection connection, Employee emp, int regionID)
        {
            try
            {
                connection.BeginTransaction();
                var newID = Convert.ToInt32(connection.InsertWithIdentity(emp));
                Console.WriteLine($"Add new employee with id {newID}");
                connection.Territorys.Where(x => x.RegionID == regionID)
                .Insert(connection.EmployeeTerritorys,
                        t => new EmployeeTerritory {
                    EmployeeID = newID, TerritoryID = t.TerritoryID
                });

                connection.CommitTransaction();
            }
            catch (Exception e)
            {
                connection.RollbackTransaction();
            }
        }