I dont know how to do this problably because both tables Have the same Key Name
- public async Task
Create([Bind( "ProductId,ProductTitle,ProductDiscription,Price,quantity,HeaderImage,MultipleImages")] ProductWithImages productWithImages) - {
- Guid NewProductId = Guid.NewGuid();
- string uploadPath = Path.Combine(_webhostenv.WebRootPath, "Images2");
- if (ModelState.IsValid)
- {
- string FileName = Guid.NewGuid().ToString() + productWithImages.HeaderImage.FileName;
- string FilePath = Path.Combine(uploadPath, FileName);
- productWithImages.HeaderImage.CopyTo(new FileStream(FilePath, FileMode.Create));
- ProductWithImagesDb newproduct = new ProductWithImagesDb()
- {
- ProductId = NewProductId,
- ProductTitle = productWithImages.ProductTitle,
- ProductDiscription = productWithImages.ProductDiscription,
- Price = productWithImages.Price,
- quantity = productWithImages.quantity,
- HeaderImage = FileName,
- };
- _context.ProductWithImagesDb.Add(newproduct);
- await _context.SaveChangesAsync();
- // succesfully adds the newproduct data into the ProductWithImagesDb table
- //---------------------------------------------------------------------------
- if (productWithImages.MultipleImages != null)
- {
- int imgorder = 0;
- foreach (IFormFile photo in productWithImages.MultipleImages)
- {
- imgorder++;
- string filename = Path.GetRandomFileName() + "_" + photo.FileName;
- string filepath = Path.Combine(uploadPath, filename);
- await photo.CopyToAsync(new FileStream(filepath, FileMode.Create));
- var productImages = new ProductImages();
- productImages.ProductId = NewProductId;
- productImages.ImagePath = filename;
- productImages.ImageOrder = imgorder;
- _context.ProductImages.Add(productImages);
- await _context.SaveChangesAsync();
- }
- }
- //once the productWithImages.MultipleImages is used
- //it crashes because it is a different table but uses the same key (ProductId)
- //-------------------------------------------------------------------------------------
- return RedirectToAction(nameof(Index));
- }
- return View();
- }
I know that once entityFramework sees the _context.add it tracks to that database table
Maybe I can remove the tracking so it can look for a new database Table
I dont know to do that or if that would even be a good idea
Jignesh KumarPosted Apr 5, 2021, 7:37 AM
osyris zosarPosted Apr 4, 2021, 10:45 AM
Jignesh KumarPosted Apr 4, 2021, 8:20 AM