int ISubCategoriesService.Add(string name, string description, int categoryId, string userId) { try { var category = categoriesService.GetById(categoryId).FirstOrDefault(); if (category == null) { return 0; } var subCategory = new SubCategory { Name = name, Description = description, Category = category, UserId = userId }; this.subCategories.Add(subCategory); subCategories.SaveChanges(); return subCategory.Id; } catch (Exception ex) { throw; } }
private void GenerateSubCategories() { for (int i = 0; i < 20; i++) { var randomName = random.GenerateRandomString(5, 15); var randomDescription = random.GenerateRandomString(10, 20); var randomCategory = random.GenerateRandomNumber(1, context.Categories.Count() - 1); var subCategory = new SubCategory() { Name = randomName, Description = randomDescription, CategoryId = randomCategory }; context.SubCategories.Add(subCategory); } context.SaveChanges(); }