/// <summary> /// Create new product. /// </summary> /// <param name="product">The product entity.</param> /// <param name="categoryName">The name of a category</param> /// <param name="supplierName">The name of a supplier</param> /// <returns>The identificator of the created em</returns> public static int CreateProduct(Product product, string categoryName, string supplierName) { using (var dbConnection = new NorthwindDataConnection()) { var productId = Convert.ToInt32(dbConnection.InsertWithIdentity(product)); int?categoryId = GetCategoryIdByName(categoryName)?.Id; int?supplierId = GetSupplierByName(supplierName)?.Id; if (!categoryId.HasValue) { var category = new Category() { Name = categoryName }; categoryId = Convert.ToInt32(dbConnection.InsertWithIdentity(category)); } if (!supplierId.HasValue) { var supplier = new Supplier() { Name = supplierName }; supplierId = Convert.ToInt32(dbConnection.InsertWithIdentity(supplier)); } dbConnection.Products .Where(p => p.Id == productId) .Set(p => p.CategoryId, categoryId.Value) .Set(p => p.SupplierId, supplierId.Value) .Update(); return(productId); } }
/// <summary> /// Create new product with assigned territories. /// </summary> /// <param name="employee">The product entity.</param> /// <param name="regionId">The region identificator.</param> /// <returns>The identificator of the created em</returns> public static int CreateEmployee(Employee employee, int regionId) { using (var dbConnection = new NorthwindDataConnection()) { var employeeId = Convert.ToInt32(dbConnection.InsertWithIdentity(employee)); var territories = GetTerritoriesByRegion(regionId); if (territories.Any()) { var emplyeeTerritoryRelations = territories.Select(territory => new EmployeeTerritoryRelations { EmployeeId = employeeId, TerritoryId = territory.Id, }).ToList(); foreach (var employeeTerritory in emplyeeTerritoryRelations) { dbConnection.Insert(employeeTerritory); } } return(employeeId); } }