我在我的数据库中创建了 5 个表:
- 类别
- 产品(FK,类别)
- 选项
- 选项组
- ProductOptions(FK、选项、选项组、产品)
如有任何混淆,我已在此处附上我的 ERD ..
我被困在我想要在我的主页上列出类别的情况。当我选择其中任何一个时,它将相应地显示选项组,并且在此选项组中,将显示选项。如何通过使用实体框架和 LINQ 查询来实现这一点?
请帮助我了解控制器逻辑。我已经映射了数据库中显示的模型。如果我使数据库关系错误,也请帮忙?
编辑
我通过建立从 Category 到 OptionGroups 的 FK 关系得到了 OptionGroup 的列表。现在我想以相同的操作方法获取每个选项组的列表。请帮助我处理这个 linq 查询,然后我想检索该特定选项的产品..也请帮助我进行该 linq 查询..
控制器
[HttpPost]
public ActionResult GetSubCategories(int btnValue)
{
Entities entity = new Entities();
HomeRoot root = new HomeRoot();
root.OptionGroups = entity.OptionGroups.Where(m => m.CategoryID == btnValue).ToList();
//Missing My logic Here
return View("SubCategories",root);
}
楷模
public partial class ProductOption
{
public int ProductOptionID { get; set; }
public int OptionID { get; set; }
public int OptionGroupID { get; set; }
public int ProductID { get; set; }
public double OptionPriceIncrement { get; set; }
public virtual OptionGroup OptionGroup { get; set; }
public virtual Option Option { get; set; }
public virtual Product Product { get; set; }
}
public partial class Product
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Product()
{
this.OrderDetails = new HashSet<OrderDetail>();
this.ProductOptions = new HashSet<ProductOption>();
}
public int ProductID { get; set; }
public string ProductSKU { get; set; }
public string ProductName { get; set; }
public Nullable<double> ProductPrice { get; set; }
public Nullable<double> ProductWeight { get; set; }
public string ProductCartDesc { get; set; }
public string ProductShortDesc { get; set; }
public string ProductLongDesc { get; set; }
public string ProductThumb { get; set; }
public string ProductImage { get; set; }
public Nullable<int> CategoryID { get; set; }
public byte[] ProductUpdateDate { get; set; }
public Nullable<double> ProductStock { get; set; }
public Nullable<byte> ProductLive { get; set; }
public Nullable<byte> ProductUnlimited { get; set; }
public string ProductLocation { get; set; }
public string ProductColor { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<OrderDetail> OrderDetails { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
public partial class OptionGroup
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public OptionGroup()
{
this.ProductOptions = new HashSet<ProductOption>();
}
public int OptionGroupID { get; set; }
public string OptionGroupName { get; set; }
public Nullable<int> CategoryID { get; set; }
public virtual ProductCategory ProductCategory { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}
public partial class Option
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Option()
{
this.ProductOptions = new HashSet<ProductOption>();
}
public int OptionID { get; set; }
public string OptionName { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ProductOption> ProductOptions { get; set; }
}