繁体   English   中英

在数据库中插入多个图像路径

[英]Insert multiple image paths in database

我在数据库中存储多个图像路径时遇到问题。 我的问题是每次尝试存储图像路径时,数据库中只保存最后一个路径。 我不知道为什么。 该代码当前将图像保存在目录中,但无法保存在数据库中。 我能做些什么来解决这个问题?

我的模型:

public class Product
{
    [Key]
    public int ProductId { get; set; }

    [Required]
    public int CategoryId { get; set; }

    public int? SubCategory { get; set; }

    [Required]
    public int BrandId { get; set; }


    
    [Display(Name = "نام محصول")]
    [Required(ErrorMessage = "لطفا{0} را وارد کنید ")]
    [MaxLength(450, ErrorMessage = " {0} نمیتواند بیشتر از {1} کاراکتر باشد")]
    public string ProductTitle { get; set; }

    [Display(Name = "شرح محصول")]
    [Required(ErrorMessage = "لطفا{0} را وارد کنید ")]
    public string ProductDiscription { get; set; }
    [Display(Name = "قیمت محصول ")]
    [Required(ErrorMessage = "لطفا{0} را وارد کنید ")]
    public int ProductPrice { get; set; }
    [MaxLength(600)]
    public string Tags { get; set; }

    public string ProductMoreInfo { get; set; }
    [MaxLength(50)]
    public string ProductMainImage { get; set; }
    [Required]
    public DateTime CreateDate { get; set; }
    public DateTime? UpdateDate { get; set; }




    #region Relation

    [ForeignKey("CategoryId")]
    public ProductCategory ProductCategory { get; set; }

    [ForeignKey("SubCategory")]
    public ProductCategory Category { get; set; }

    public Brand Brand { get; set; }

    public List<ProductAttribute> ProductAttributes { get; set; }
    public ProductGallery ProductGallery { get; set; }
    #endregion

public class ProductGallery
{
    [Key]
    public int ImageId { get; set; }

    public int ProductId { get; set; }
    [Display(Name = "نام تصویر")]
    public string ImageUrl { get; set; }

   



    #region Relation

    public Product Product { get; set; }

    #endregion
}

这是我所做的:

 public int AddProduct(Product product, IFormFile ImgProduct, List<IFormFile> moreImg)
    {
        product.CreateDate = DateTime.Now;
        product.ProductMainImage = "";
        //TODO Check Image 
        if (ImgProduct != null)
        {
            product.ProductMainImage = NameGenerator.GenerateUniqCode() + Path.GetExtension(ImgProduct.FileName);
            string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Product/image",
                product.ProductMainImage);
            using (var stream = new FileStream(imagePath, FileMode.Create))
            {
                ImgProduct.CopyTo(stream);
            }

            _context.Add(product);
        }

        _context.SaveChanges();
      
            AddProductImageGallery(product.ProductId, moreImg);
        return product.ProductId;
        
    }

    public bool AddProductImageGallery(int productId, List<IFormFile> images)
    {
        List<ProductGallery> gallery = new List<ProductGallery>();
       //   gallery.ImageUrl = "";
        //TODO Check Image 
        if (images != null)
        {
            foreach (IFormFile img in images)
            {
                ProductGallery gallery1=new ProductGallery();
                gallery1.ProductId = productId;
                gallery1.ImageUrl = NameGenerator.GenerateUniqCode() + Path.GetExtension(img.FileName);
                string imagePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Product/image",
                    gallery1.ImageUrl);

               
               
                using (var stream = new FileStream(imagePath, FileMode.Create))
                {
                    img.CopyTo(stream);
                }
                gallery.Add(gallery1);
            }
            _context.AddRange(gallery);
            _context.SaveChanges();

        }

       
        return true;
    }

您为模型定义的关系是一对一 + 您为主图像添加的属性。 因此,您的模型不能有超过 2 个图像(一个在您的主图像中,另一个在您的产品库中)。 您可以在产品和产品库之间建立一对多的关系。

您可以使用此链接获取有关一对多的更多信息。 您的控制器很好,为了获取相关数据,您可以在使用 EF Core 查询时使用 .include。

https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key

public class Product
{
    //other properties
    public int Id { get; set; }
    public ICollection<ProductGallery> ProductGalleries { get; set; } = new HashSet<ProductGallery>();
}

public class ProductGallery
{
    //product galleries other properties

    public int ProductdId { get; set; }
    public virtual Product Product { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM