【问题标题】:Insert multiple image paths in database在数据库中插入多个图像路径
【发布时间】:2020-09-05 08:21:30
【问题描述】:

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

我的模型:

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;
    }

【问题讨论】:

  • 首先 ImageUrl 在您的产品类中不存在...您可能忘记在此处添加它
  • 以下是关于:stackoverflow.com/questions/58122947/… 的答案
  • 我把 url 保存在图片 ProductMainImage 中,

标签: c# asp.net entity-framework asp.net-core entity


【解决方案1】:

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

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

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; }
}

【讨论】:

  • 或者还有另一种方式,您可以在产品和产品库之间建立一对一关系,在产品库和图像之间再次建立一对多关系(您必须制作的新模型),您可以采取所有与保存图像路径相关的属性。
  • 感谢重播。没错,问题出在我的关系上。我将其定义为一对一而不是一对多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 2023-04-05
  • 2014-05-28
  • 2019-07-27
相关资源
最近更新 更多