【问题标题】:Having trouble in retrieving data from database in asp.net mvc在 asp.net mvc 中从数据库中检索数据时遇到问题
【发布时间】:2018-08-11 05:22:50
【问题描述】:

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

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

如果有任何混淆,我附上了我的 ERD here..

我被困在我想要在我的主页上列出类别列表的情况下。当我选择其中任何一个时,它将相应地显示选项组,并且在此选项组中,将显示选项。如何通过使用实体框架和 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; }
}

【问题讨论】:

  • 我确定您尝试了一些方法。请展示您的努力,以便我们了解具体您需要帮助的地方。目前尚不清楚您在问什么(或为什么要问)。
  • @GertArnold 我已经编辑了我的帖子,请检查我的代码并帮助我.. 提前致谢:)

标签: c# asp.net-mvc entity-framework linq


【解决方案1】:

1.

在你的情况下,这样写来获得选定的类别选项组列表:

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

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

2.

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

【讨论】:

  • 正是我想将类别与 OptionGroup 直接关联的原因。但我问是否有没有这个参考的解决方案
  • 您的解决方案是第一个。其次是我的建议
  • 您能查看我的帖子并回答我的问题吗? @ebattulga
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 2023-03-11
  • 2021-02-24
  • 2016-05-24
相关资源
最近更新 更多