繁体   English   中英

在ASP.NET MVC中从数据库检索数据时遇到问题

[英]Having trouble in retrieving data from database in asp.net mvc

我在数据库中创建了5个表:

  1. 分类
  2. 产品(FK,类别)
  3. 选项
  4. OptionGroups
  5. 产品选项(FK,选项,选项组,产品)

如有任何混淆,请在此处附加我的ERD。

我一直想在主页上找到类别列表。 当我选择其中任何一个时,它将相应地显示选项组,并且在该选项组中将显示选项。 如何通过使用实体框架和LINQ查询来实现这一点?

请帮助我控制器逻辑。 我已经映射了数据库中显示的模型。 如果我使数据库关系错误,也请帮忙?

编辑

我已经通过建立从类别到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; }
}

1。

在您的情况下,请这样编写以获取所选类别选项组列表:

var SelectedCategoryOptionGroup= Context.ProductOptions
                .Where(po=>po.Product.CategoryID ==SelectedCategoryID)
                .Select(po=>po.OptionGroup)
                .Distinct();

通过Entityframework with Lazy loading技术Entityframework with Lazy loading SelectedCategoryID是您的主页选择类别ID。

2。

否则:如果您的OptionGroupCategory直接相关,则必须将OptionGroup直接引用到Category。 在这种情况下,您更容易实现目标。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM