c# - 在数据库中插入多个图像路径
问题描述
我在数据库中存储多个图像路径时遇到问题。我的问题是每次我尝试存储图像路径时,只有最后一个路径保存在数据库中。我不知道为什么。该代码当前将图像保存在目录中,但无法保存在数据库中。我能做些什么来解决这个问题?
我的模型:
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 张图像(一张在您的主图像中,另一张在您的产品库中)。您可以在产品和产品库之间建立一对多的关系。
您可以使用此链接了解有关一对多的更多信息。您的控制器很好,为了获取相关数据,您可以使用 .include 在使用 EF Core 进行查询时使用。
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; }
}
推荐阅读
- spring - spring boot - 没有主要工件
- powershell - Powershell New-LocalUser 无法正常工作
- java - 是否有从其他 Activity 类访问 MainActivity 的解决方案?
- java - spark-submit: java.lang.NoSuchMethodError: org.apache.spark.SparkContext$.$lessinit$greater$default$6()Lscala/collection/Map;
- r - 使用roxygen2生成命名空间:一个小例子或模板
- angular - 如何在 Highcharts 中将图像用于图表
- android - lateinit 属性 ViewModelFactory 尚未初始化
- python - SQL查询如何使用两个日期变量?'date' 和 regexp_replace('date', '-', ''),1,8)
- r - 在 R 3.6.1 中安装包的问题
- excel - 创建新工作表仅适用于下一个数据,它会引发错误“运行时错误 9”