C# 在数据库中插入多个映像路径
我在数据库中存储多个图像路径时遇到问题。我的问题是每次尝试存储图像路径时,数据库中只保存最后一个路径。我不知道为什么。代码当前将图像保存在目录中,但无法保存在数据库中。我能做些什么来解决这个问题 我的模型:C# 在数据库中插入多个映像路径,c#,asp.net,entity-framework,asp.net-core,entity,C#,Asp.net,Entity Framework,Asp.net Core,Entity,我在数据库中存储多个图像路径时遇到问题。我的问题是每次尝试存储图像路径时,数据库中只保存最后一个路径。我不知道为什么。代码当前将图像保存在目录中,但无法保存在数据库中。我能做些什么来解决这个问题 我的模型: public class Product { [Key] public int ProductId { get; set; } [Required] public int CategoryId { get; set; } public int? S
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 ProductId{get;set;}
[必需]
public int CategoryId{get;set;}
公共int?子类别{get;set;}
[必需]
public int BrandId{get;set;}
[显示(Name=“ناممحول”)]
[必需(ErrorMessage=“لطفااااکنید”)]
[MaxLength(450,ErrorMessage=“{0}
公共字符串ProductTitle{get;set;}
[显示(Name=“شحمحول”)]
[必需(ErrorMessage=“لطفااااکنید”)]
公共字符串ProductDescription{get;set;}
[显示(Name=“قیمتمحل”)]
[必需(ErrorMessage=“لطفااااکنید”)]
public int ProductPrice{get;set;}
[最大长度(600)]
公共字符串标记{get;set;}
公共字符串ProductMoreInfo{get;set;}
[MaxLength(50)]
公共字符串ProductMainImage{get;set;}
[必需]
公共日期时间CreateDate{get;set;}
公共日期时间?更新日期{get;set;}
#区域关系
[外国钥匙(“类别”)]
公共产品类别ProductCategory{get;set;}
[外键(“子类别”)]
公共产品类别{get;set;}
公共品牌品牌{get;set;}
公共列表ProductAttributes{get;set;}
public ProductGallery ProductGallery{get;set;}
#端区
公共类产品库
{
[关键]
公共int-ImageId{get;set;}
public int ProductId{get;set;}
[显示(名称:)]
公共字符串ImageUrl{get;set;}
#区域关系
公共产品产品{get;set;}
#端区
}
以下是我所做的:
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;
}
public int AddProduct(产品产品、IFormFile ImgProduct、列表更多img)
{
product.CreateDate=DateTime.Now;
product.ProductMainImage=“”;
//TODO检查图像
if(ImgProduct!=null)
{
product.ProductMainImage=NameGenerator.GenerateUniqCode()+Path.GetExtension(ImgProduct.FileName);
字符串imagePath=Path.Combine(Directory.GetCurrentDirectory(),“wwwroot/Product/image”,
product.ProductMainImage);
使用(var stream=newfilestream(imagePath,FileMode.Create))
{
ImgProduct.CopyTo(流);
}
_添加(产品);
}
_SaveChanges();
添加ProductImageGallery(product.ProductId,moreImg);
返回product.ProductId;
}
公共bool AddProductImageGallery(int-productId,列表图像)
{
列表库=新列表();
//gallery.ImageUrl=“”;
//TODO检查图像
如果(图像!=null)
{
foreach(图像中的img文件)
{
ProductGallery1=新的ProductGallery();
gallery1.ProductId=ProductId;
gallery1.ImageUrl=NameGenerator.GenerateUniqCode()+Path.GetExtension(img.FileName);
字符串imagePath=Path.Combine(Directory.GetCurrentDirectory(),“wwwroot/Product/image”,
gallery1.ImageUrl);
使用(var stream=newfilestream(imagePath,FileMode.Create))
{
img.CopyTo(流);
}
增补画廊(画廊1);
}
_context.AddRange(gallery);
_SaveChanges();
}
返回true;
}
您为模型定义的关系是一对一+您为主映像添加的属性。因此,您的模型不能有两个以上的映像(一个在主映像中,另一个在productgallery中)。您可以在产品和productgallery之间建立一对多关系
您可以使用此链接获取有关一对多的更多信息。您的控制器很好,可以获取您可以使用的相关数据。包括在使用EF Core查询时
公共类产品
{
//其他属性
公共int Id{get;set;}
公共ICollection ProductGalleries{get;set;}=new HashSet();
}
公共类产品库
{
//产品库及其他物业
public int ProductdId{get;set;}
公共虚拟产品产品{get;set;}
}
Product类中不存在所有这些ImageUrl的第一个…您可能忘记在此处添加它,下面是一个关于以下内容的答案:我将url保存在image ProductMainImage中,或者有另一种方法,您可以在产品和产品库之间建立一对一关系,在产品库和图像之间建立一对多关系(您必须制作的新模型)并且您可以获取与保存图像路径相关的所有属性。感谢重播。没错,问题出在我的关系上。我将其定义为一对一,而不是一对多。
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; }
}