【问题标题】:ASP.NET Core MVC Product Database with Multiple Prices具有多种价格的 ASP.NET Core MVC 产品数据库
【发布时间】:2017-10-05 04:28:09
【问题描述】:

我正在将我的一个网站从 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 以来,我一直试图弄清楚如何检查每个项目的价格数量并适当地标记它们。我尝试使用数组,但它们不起作用,因为实体框架模型类中的每个变量类型都连接到 SQL Server 数据类型。我不想使用一堆 if/else 语句来计算与每个产品相关联的价格并再次适当地标记它们。我的产品类与带有实体框架和 CSS 的 ASP.NET MVC 中的产品类没有什么不同。

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; }
}

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

【问题讨论】:

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


    【解决方案1】:

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

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

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

    这确实是基本的Linq功能,你似乎很缺乏经验,所以我建议阅读一些关于实体框架的教程。 EntityFramework 核心在细节上有很大不同,但就查询而言,其工作原理与传统实体框架相同。

    【讨论】:

    • 我在想类似的事情;创建一个包含该产品标签和价格的价格表,然后将这两个表与对产品表的引用和对价格表中价格 ID 的另一个引用进行链接。 ` //添加第三个表来保存价格信息?` ` //public int? PriceTableID { 获取;放; }` 既然有多个价格,我是否也需要添加这一行? public virtual Category Category { get; set; }
    猜你喜欢
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多