简体   繁体   English

在代码中加入表第一个实体框架MVC c#

[英]trouble joining tables in code first entity framework MVC c#

Hi I have 2 questions both related. 嗨,我有两个相关的问题。 First I a having trouble setting up code first EF tables that link. 首先,我无法设置代码,首先是链接的EF表。 I have a product model, productimage model and category model as so 我有一个产品型号,产品图像模型和类别模型

 public class Product
    {
        public int ProductId { get; set; }
        public string SKU { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string DescriptionShort { get; set; }


        public decimal Weight { get; set; }
        public string Size { get; set; }
        public decimal Price { get; set; }
        public int WebStatus { get; set; }
        public int CategoryId { get; set; }
        public decimal SalePrice { get; set; }
        public DateTime SaleDateExpires { get; set; }
        public Boolean FeaturedProduct { get; set; }
        public Boolean NewProduct { get; set; }
        public Boolean TopRated { get; set; }
        public Boolean BestSellers { get; set; }
        public string ImageUrl { get; set; }
        public string ImageName { get; set; }
        public string ImageExtension { get; set; }

        public Category Category { get; set; }
        public ICollection<ProductImage> ProductImage { get; set; }   
    }   

 public class ProductImage
        {        
            public int ProductImageID { get; set; }
            public int ProductID { get; set; }
            public string ImageType { get; set; }
            public string ImageUrl { get; set; }
            public string ImageName { get; set; }

            public Product Product { get; set; }      
        }

public class Category
    {
        public int CategoryId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

Here is model passed from the controller code 这是从控制器代码传递的模型

var products = _context.Products.Include(c => c.Category).Include(i => i.ProductImage);

But I can only get the products and the category. 但我只能得到产品和类别。 I cant figure out how to have access to the productimage fields. 我无法弄清楚如何访问productimage字段。

Question 1. How can I display the products, category and all images associated with that product from the productimage model. 问题1.如何从productimage模型中显示与该产品相关的产品,类别和所有图像。

My view is using 我的观点是使用

@model IEnumerable<shop.Models.Product>

Since I don't need to edit any data it's all for view purposes. 由于我不需要编辑任何数据,因此全部用于查看目的。 I don't get intelisense or access to the productimage model. 我没有获得知识产权或访问productimage模型。

I have tried this 我试过这个

<img class="full-width img-responsive" src="@product.ProductImage.>

but it doesn't work. 但它不起作用。 Is this because it is a collection and I have to loop thru the data some how? 这是因为它是一个集合,我必须通过数据循环一些如何?

Question 2. Once I get that working, I want to display different products in different sections. 问题2.一旦我开始工作,我想在不同的部分显示不同的产品。 ex ( new products, sale products , popular products) in different areas of the same page. ex(新产品,促销产品,热门产品)在同一页面的不同区域。 What is the best way and how do I do this. 什么是最好的方式,我该怎么做。 My guess is different viewmodels, combined viewmodels, or one model and then looping thru the data with if statements. 我的猜测是不同的视图模型,组合视图模型或一个模型,然后使用if语句循环数据。

Please give an example thank you 请举个例子谢谢

First, I would pluralize to ProductImages - much clearer that it is a collection. 首先,我会复制到ProductImages - 更清楚它是一个集合。 Next, since it is a collection you would typically use a grid or table to display the data. 接下来,由于它是一个集合,您通常会使用网格或表来显示数据。

As for grouping by type, you could just use some LINQ to filter. 至于按类型分组,你可以使用一些LINQ来过滤。

Here is a bootstrap styled example: 这是一个bootstrap风格的例子:

<h2>New Products</h2>
<table class="table table-condensed table-striped">
    <tr>
        <th class="col-md-4">Product Name</th>
        <th class="col-md-4">SKU</th>
        <th class="col-md-4">Description</th>
    </tr>
    // filter just the new products. could add an `if` to check if empty
    @foreach (var product in Model.Products.Where(pi => pi.NewProduct))
    {
        <tr>
            <td>
                @Html.DisplayFor(m => product.Name)
            </td>
            <td>
                @Html.DisplayFor(m => product.SKU)
            </td>
            <td>
                @Html.DisplayFor(m => product.Description)
            </td>
        </tr>
        <tr>   // Add another row (or column) for image(s)
            <table>
                // header here
                // loop thru multiple images for this product
                @foreach (var image in product.ProductImages)
                {
                    <tr>
                        <td>image.Name</td>
                        // other columns
                    </tr>
                }
            <table>

        </tr>

    }

</table>

// Repeat for other sections.


@foreach (var product in Model.Where(pi => pi.FeaturedProduct == true))
            {
                foreach (var image in product.ProductImage.Where(it => it.ImageType == "Thumb"))
                {
                    <img class="full-width img-responsive" src="@image.ImageUrl" >

            }

With help from Steve this is the code I ended up using that worked. 在史蒂夫的帮助下,这是我最终使用的代码。

@foreach (var product in Model.Where(pi => pi.FeaturedProduct == true))
            {
                foreach (var image in product.ProductImage.Where(it => it.ImageType == "Thumb"))
                {
                    <img class="full-width img-responsive" src="@image.ImageUrl" >

                }

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

相关问题 首先在实体框架代码中连接具有一对多关系的两个表 - Joining two tables with one to many relatipnship in entity framework code first 在Code First Entity Framework中指定除dbo之外的SQL用户名(C#ASP.NET MVC 3) - Specify an SQL username other than dbo in Code First Entity Framework ( C# ASP.NET MVC 3 ) 如何使用流畅的API在MVC实体框架C#中编写(第一个)以下三元关系? - How to code (first) the following ternary relationship in MVC Entity Framework C# with fluent API? C#实体框架联接,联接两个表时选择存在值 - C# Entity Framework join, choose the exist value when joining 2 tables 具有类/数据库的循环引用(C#,首先是实体框架代码) - Circular reference with Classes/Database (C#, Entity Framework Code First) C#-实体框架-大种子数据代码优先 - C# - Entity Framework - Large seed data code-first C#实体框架(代码优先),在模型上实现CRUD操作 - C# Entity Framework (Code first), Implementing CRUD Operations on Model C#实体框架代码首次创建控制器 - C# Entity Framework Code First-create controller C# - 实体框架代码优先,延迟加载不起作用 - C# - Entity Framework Code first, lazy loading not working C#交互式窗口中的实体框架Code-First(Oracle) - Entity framework Code-First in c# interactive window (Oracle)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM