title: Save Image source to database using .net core 5
body: <p>I'm trying to create a shop where I have a product that has an array of photos, and I want to save these images or their source (where the picture itself is located in the project folder) into the database.</p>
  <p>I was thinking about creating a separate entity for the picture and a one-to-many relationship between the picture and the product.</p>
  <p>This is my code -  model class:</p>
  <pre><code>public class Product
  {
      [Key]
      [ScaffoldColumn(false)]
      public int Id { get; set; }
  
      [Required]
      public string ProductName { get; set; }
  
      public double? UnitPrice { get; set; }
      public string ImageName { get; set; }
  
      [NotMapped]
      public IFormFile ImageFile { get; set; }
      [NotMapped]
      public string ImageSrc { get; set; }
  
      public string Description { get; set; }
  
      public IList&lt;Category&gt; Categories { get; set; } = new List&lt;Category&gt;();
      [JsonIgnore]
      public bool IsDisable { get; set; } = false;
  }
  </code></pre>
  <p>Controller</p>
  <pre><code>[HttpPost]
  [Authorize(Role.Admin)]
  public async Task&lt;IActionResult&gt; CreateProduct([FromForm] ProductDTO dTO)
  {
      var currentUser = (User)HttpContext.Items[&quot;User&quot;];
  
      if (currentUser.Role != Role.Admin)
          return  StatusCode(403, new { message = &quot;Forbidden&quot; });
  
      dTO.ImageName = await SaveImage(dTO.Image);
      var product = service.CreateProduct(dTO);
  
      return Created(&quot;createproduct&quot;, product);
  }
  
  [NonAction]
  public async Task&lt;string&gt; SaveImage(IFormFile imageFile)
  {
      string imageName = new String(Path.GetFileNameWithoutExtension(imageFile.FileName).Take(10).ToArray()).Replace(' ', '-');
      imageName = imageName + DateTime.Now.ToString(&quot;yymmssfff&quot;) + Path.GetExtension(imageFile.FileName);
      var imagePath = Path.Combine(environment.ContentRootPath, &quot;wwwroot&quot;, imageName);
  
      using (var fileStream = new FileStream(imagePath, FileMode.Create))
      {
          await imageFile.CopyToAsync(fileStream);
      }
  
      return imageName;
  }
  </code></pre>
  <p>Repository</p>
  <pre><code>public Product CreateProduct(Product product)
  {
      var produc = new Product
          {
              ProductName = product.ProductName,
              Description = product.Description,
              UnitPrice = product.UnitPrice,
              ImageName=product.ImageName
          };
  
      foreach (var item in product.Categories)
      {
          var category = context.Categories
                                .Where(w =&gt; w.Id == item.Id)
                                .FirstOrDefault();
  
          produc.Categories.Add(category);
      }
  
      context.Products.Add(produc);
      context.SaveChanges();
  
      return new Product
          {
              Id = produc.Id,
              ProductName = produc.ProductName,
              Description = produc.Description,
              UnitPrice = produc.UnitPrice,
              Categories = produc.Categories,
              ImageName=produc.ImageName
          };
  }
  </code></pre>
  <p>Service</p>
  <pre><code>public Product CreateProduct(ProductDTO dTO)
  {
      var product = new Product
          {
              Description = dTO.Description,
              ProductName = dTO.Name,
              UnitPrice = dTO.UnitPrice,
              Categories = dTO.Categories,
              ImageName=dTO.ImageName,
          };
  
      var pro = repos.CreateProduct(product);
  
      return pro;
  }
  </code></pre>
  <p>how can I save a source to a picture in the database?</p>