public void Add(SubCategoryViewModel vm) { if (vm.Id == 0) { var testList = new List <SubCategoryAttributeGroup>(); foreach (var attrGrId in vm.AttributeGroupId) { var newSubCategoryAttributeGroup = new SubCategoryAttributeGroup { SubCategoryId = vm.SubCategoryId, AttributeGroupId = attrGrId, }; testList.Add(newSubCategoryAttributeGroup); } var newSubCategory = new SubCategory { Name = vm.Name, CategoryId = vm.CategoryId, SubCategoryAttributeGroups = testList, AddedDate = DateTime.Now, UpdatedDate = DateTime.Now, Published = false, Version = 1, ModifiedByUser = vm.ModifiedByUser }; _ctx.SubCategories.Add(newSubCategory); } _ctx.SaveChanges(); }
public IActionResult AtttributeGroupSubCategory(AttributeGroup group, int id) { SubCategoryAttributeGroup subCategoryAttributeGroup = new SubCategoryAttributeGroup(); { subCategoryAttributeGroup.AttributeGroupId = group.AttributeGroupId; subCategoryAttributeGroup.SubCategoryId = id; } _context.Add(subCategoryAttributeGroup); _context.SaveChangesAsync(); return(RedirectToAction("SubCategoryDetails", new { id = id })); }
public static void FillIfEmpty(ApplicationDbContext ctx) { // The list variables created for the Seed(): var categoryList = new List <Category>(); var subCategoryList = new List <SubCategory>(); var productList = new List <Product>(); var attributeGroupList = new List <AttributeGroup>(); var oneAttributeList = new List <OneAttribute>(); var subCategoryAttributeGroupList = new List <SubCategoryAttributeGroup>(); // Attribute: var oneAttribute = new OneAttribute { Name = "Color", Description = "The specific color of the product.", Type = "string" }; ctx.OneAttributes.Add(oneAttribute); ctx.SaveChanges(); // AttributeGroup: var attributeGroup = new AttributeGroup { Name = "Style", Description = "The style of the product.", OneAttributes = oneAttributeList }; ctx.AttributeGroups.Add(attributeGroup); ctx.SaveChanges(); // Product: var product = new Product { Name = "Ralph Lauren", Description = "Slim fit, long sleeved shirt.", Price = "999", UpdatedDate = DateTime.Now.Date, AddedDate = DateTime.Today, Published = false, Version = 1, ModifiedByUser = "******" }; productList.Add(product); // SubCategory: var subCategory = new SubCategory { Name = "Long sleeve", Products = productList, SubCategoryAttributeGroups = subCategoryAttributeGroupList, UpdatedDate = DateTime.Now.Date, AddedDate = DateTime.Today, Published = false, Version = 1, ModifiedByUser = "******" }; subCategoryList.Add(subCategory); // SubCategoryAttributeGroups: var subCategoryAttributeGroups = new SubCategoryAttributeGroup { SubCategory = ctx.SubCategories.FirstOrDefault(x => x.Id == 1), AttributeGroup = ctx.AttributeGroups.FirstOrDefault(x => x.Id == 1) }; subCategoryAttributeGroupList.Add(subCategoryAttributeGroups); // Category: if (!ctx.Categories.Any()) { var category = new Category { Name = "Shirts", SubCategories = subCategoryList, UpdatedDate = DateTime.Now.Date, AddedDate = DateTime.Now, Published = false, Version = 1, ModifiedByUser = "******" }; ctx.Categories.AddRange(category); } ; ctx.SaveChanges(); // Connect a product to a attribute to productoneattributevalues: if (!ctx.ProductOneAttributeValues.Any()) { var productTryout = ctx.Products.FirstOrDefault(x => x.Id == 1); var attributeTryout = ctx.OneAttributes.FirstOrDefault(x => x.Id == 1); var productAttributeValue = new ProductOneAttributeValue { Value = "White", Product = productTryout, OneAttribute = attributeTryout }; ctx.ProductOneAttributeValues.Add(productAttributeValue); ctx.SaveChanges(); } }