osyris zosar

osyris zosar

  • NA
  • 289
  • 27.6k

How to add data into 2 different tables with EnityFramework

Apr 4 2021 6:46 AM
I would like to add new data rows with entityframworks to 2 different tables
I dont know how to do this problably because both tables Have the same Key Name
 
  1.  public async Task Create([Bind("ProductId,ProductTitle,ProductDiscription,Price,quantity,HeaderImage,MultipleImages")] ProductWithImages productWithImages)  
  2.         {  
  3.                 Guid NewProductId = Guid.NewGuid();  
  4.                 string uploadPath = Path.Combine(_webhostenv.WebRootPath, "Images2");  
  5.   
  6.                 if (ModelState.IsValid)  
  7.                 {  
  8.                     string FileName = Guid.NewGuid().ToString() + productWithImages.HeaderImage.FileName;  
  9.                     string FilePath = Path.Combine(uploadPath, FileName);  
  10.                     productWithImages.HeaderImage.CopyTo(new FileStream(FilePath, FileMode.Create));  
  11.   
  12.                     ProductWithImagesDb newproduct = new ProductWithImagesDb()  
  13.                     {  
  14.                         ProductId = NewProductId,  
  15.                         ProductTitle = productWithImages.ProductTitle,  
  16.                         ProductDiscription = productWithImages.ProductDiscription,  
  17.                         Price = productWithImages.Price,  
  18.                         quantity = productWithImages.quantity,  
  19.                         HeaderImage = FileName,  
  20.                           
  21.                     };  
  22.   
  23.                 _context.ProductWithImagesDb.Add(newproduct);
  24.                 await _context.SaveChangesAsync();  
  25.   
  26. // succesfully adds the newproduct data into the ProductWithImagesDb table  
  27. //---------------------------------------------------------------------------  
  28.   
  29.                 if (productWithImages.MultipleImages != null)  
  30.                     {  
  31.                         int imgorder = 0;  
  32.   
  33.                         foreach (IFormFile photo in productWithImages.MultipleImages)  
  34.                         {  
  35.                                 imgorder++;  
  36.                                 string filename = Path.GetRandomFileName() + "_" + photo.FileName;  
  37.                                 string filepath = Path.Combine(uploadPath, filename);  
  38.                                 await photo.CopyToAsync(new FileStream(filepath, FileMode.Create));  
  39.   
  40.                         var productImages = new ProductImages();  
  41.                         productImages.ProductId = NewProductId;  
  42.                         productImages.ImagePath = filename;  
  43.                         productImages.ImageOrder = imgorder;  
  44.   
  45.                         _context.ProductImages.Add(productImages);  
  46.                         await _context.SaveChangesAsync();
  47.                         }  
  48.                     }  
  49. //once the productWithImages.MultipleImages is used  
  50. //it crashes because it is a different table but uses the same key (ProductId)  
  51. //-------------------------------------------------------------------------------------  
  52.                     return RedirectToAction(nameof(Index));  
  53.                 }  
  54.             return View();  
  55.         }  
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 
 
 

Answers (3)