具有多个价格的ASP.NET核心MVC产品数据库

时间:2017-10-05 04:28:10

标签: c# sql-server asp.net-mvc entity-framework asp.net-core

我正在将我的一个站点从WebForms升级到MVC Core。您能否就如何创建数据库给我一些建议?以下是Rabbit Bikes Products页面后面的.VB文件中的一些示例代码。它非常混乱,因为他们的很多产品都有两个或更多的价格。

For Each prod As Product In myProducts
        'Beginning Layout
        temp &= "<article class=""dbItem""><figure class=""left dbImage"">"

        temp &= "<img src=""" & prod.ImagePath & """ alt=""" & prod.Name & """ title=""" & prod.Name & """ style=""width: 100%;"" class=""pull-left img-rounded""></figure>"
        temp &= "<h2>" & prod.Name & "</h2>"
        temp &= "<div class="" scroller""><p>" & prod.Description & "</p></div>"
        If Not prod.Description.Contains("shuttle") And Not prod.Name = "Trail Pass" Then
            temp &= "<h3 style=""text-transform: uppercase;"">Call to reserve yours today!</h3>"
        Else
            temp &= "<h3 style=""text-transform: uppercase;"">Call to purchase one today!</h3>"
        End If
        temp &= "<br /><div style=""text-align: center;"">"
        If prod.Description.Contains("shuttle") Or prod.Description.Contains("frequent rider") Then
            temp &= "<span class=""oSnap"">One Person:</span> " & prod.TwoHrPrice.ToString("c") & "<br/>"
            temp &= "<span class=""oSnap"">2 or More <abbr title=""people"">ppl.</abbr>:</span> " & prod.FourHrPrice.ToString("c") & "</p>"
        End If
        If prod.TwoHrPrice = 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<p>Free with purchase!</p>"
        End If
        If prod.FourHrPrice <> 0 And Not prod.Description.Contains("shuttle") And Not prod.Description.Contains("frequent rider") Then
            temp &= "<p><span class=""oSnap"">Half Day Price:</span> " & prod.FourHrPrice.ToString("c") & "<br/>"
        End If
        If prod.OneDayPrice <> 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<span class=""oSnap"">One Day Price:</span> " & prod.OneDayPrice.ToString("c") & "<br/>"
        End If
        If prod.TwoDayPrice <> 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<span class=""oSnap"">Two Day Price:</span> " & prod.TwoDayPrice.ToString("c") & "<br/>"
        End If
        If prod.WeekPrice <> 0 And Not prod.Description.Contains("shuttle") Then
            temp &= "<span class=""oSnap"">Week Price:</span> " & prod.WeekPrice.ToString("c") & "</p>"
        End If

        'End Layout
        temp &= "</div></article>"
    Next

自从切换到ASP.NET Core 1.1和实体框架以来,我一直试图弄清楚如何检查每个项目的价格数量并对其进行适当标记。我尝试使用数组,但它们不能工作,因为Entity Framework Model类中的每个变量类型都连接到SQL Server数据类型。我不想使用一堆if / else语句来计算与每种产品相关的价格数量,并再次适当地标记它们。我的Product类与ASP.NET MVC中的Entity Framework和CSS不同。

public class Product
{
    public int ID { get; set; }
    public string Image { get; set; }
    [Display(Name = "Product Name")]
    public string Name { get; set; }
    public string Description { get; set; }
    public string PriceLabels { get; set; }
    public decimal Prices { get; set; }
    //Add a third table to hold price information?
    //public int? PriceTableID { get; set; }
    public int? CategoryID { get; set; }
    [Display(Name = "Category Name")]
    public virtual Category Category { get; set; }
}

如果您对此问题有任何见解,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

你的问题到底是什么?通用的“建议”在这里不是主题。您应该询问具体问题以获得具体答案。如果您的问题是如何计算价格,您只需进行查询并使用计数方法linq。例如,假设您的数据库上下文被称为db,而您的products表是Products。

var count = db.Products.Where(x => x.ProductId == yourproductid).Count();

在这种情况下,每个产品/价格组合都有多个条目。另一种选择是根据价格创建一对多的产品,然后分别查询价格。

这是Linq的基本功能,你似乎非常缺乏经验,所以我建议阅读一些关于实体框架的教程。 EntityFramework核心在细节方面有点不同,但就查询作为传统实体框架而言,通常工作方式相同。