【发布时间】:2020-11-25 00:35:30
【问题描述】:
基于 ASP.NET Core 3.0 的电子商务网站
有两个模型类:
- 产品
- 图片
单个产品的多个图像存储在图像表中。我正在尝试创建一个所有产品页面,但我正在努力将图像与产品代码匹配并将其传递给 View 以显示商店中的所有产品以及来自 Images.cs 和产品标题的缩略图及其价格Product.cs 表。我将如何匹配来自两个模型类的数据,并确保所有匹配的图像和产品都得到相关显示。
图像.cs
public class Image
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ImageID { get; set; }
public string ProductCode { get; set; }
public virtual Product Product { get; set; }
public byte[] Pic { get; set; }
public string FileName { get; set; }
public string ContentType { get; set; }
}
产品.cs
public class Product
{
public Product()
{
ICollection<Category> Categories = new HashSet<Category>();
ICollection<Image> Images = new HashSet<Image>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ProductID{ get; set; }
[Required, StringLength(150)]
public string ProductCode{ get; set; }
[Required, StringLength(150)]
public string Title { get; set; }
[StringLength(500)]
public string Description { get; set; }
public int Price { get; set; }
[StringLength(150)]
public string Type { get; set; }
[ForeignKey("Category")]
[Required]
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Category> Categories { get; set; }
public virtual ICollection<Image> Images { get; set; }
}
想要创建这样的东西:
【问题讨论】:
-
每个产品一张,还是多张?
-
对于“类别”(sp?)您需要确定这些关系是一对一、一对多还是多对多关系。现在,您正在使用外键作为类别,这似乎是一对一的,但是您也有一个类别列表。 docs.microsoft.com/en-us/ef/core/modeling/…
标签: asp.net-core entity-framework-core