【问题标题】:Navigation Property Is Returning Null Properties导航属性返回空属性
【发布时间】:2020-10-25 04:00:42
【问题描述】:

因此项目在 .Net Framework 中运行良好,但现在我尝试在 .Net Core 3 中进行,但无法正常运行。

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string ImageURL { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}
public class BasketLine
{
    public int Id { get; set; }
    public string BasketId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public DateTime DateCreated { get; set; }
    public Product Product { get; set; }
}

所以我可以添加到 BasketLine 表并计算所有产品的总数,但是当我加载购物车页面时,我得到一个 NullReferenceException

如果我在购物车页面上注释掉 @Model.BasketLines[i].Product.Name 或任何导航属性,购物车页面将正常工作

这是 addBasket 方法

public void AddToBasket(int productId, int quantity)
    {
        var basketLine = db.BasketLines.FirstOrDefault(b => b.BasketId == BasketId &&
                                                                    b.ProductId == productId);
        if (basketLine == null)
        {
            basketLine = new BasketLine
            {
                ProductId = productId,
                BasketId = BasketId,
                Quantity = quantity,
                DateCreated = DateTime.Now
            };
            db.BasketLines.Add(basketLine);
        }
        else
        {
            basketLine.Quantity += quantity;
        }
        db.SaveChanges();
    }

我错过了什么是因为我不知道我在这里做错了什么

添加了 Include 并没有改变任何事情,然后发生了一件奇怪的事情,我改变了这个

<a asp-area="Customer"
  asp-controller="Shop"
  asp-action="Details"
  asp-route-id="@Model.BasketLines[i].ProductId">
  @Model.BasketLines[i].Product.Name
</a>

到这里

@Html.ActionLink(Model.BasketLines[i].Product.Name, "Details", "Products", new { id = Model.BasketLines[i].ProductId }, null)<br />

然后一切都尝试了几次,然后我又遇到了同样的错误

AspNetCore.Areas_Customer_Views_Basket_Index.<ExecuteAsync>b__15_0() in Index.cshtml
+
                    @Html.ActionLink(Model.BasketLines[i].Product.Name, "Details", "Products", new { id = Model.BasketLines[i].ProductId }, null)<br />
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.GetChildContentAsync(bool useCachedResult, HtmlEncoder encoder)
Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper.ProcessAsync(TagHelperContext context, TagHelperOutput output)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner.<RunAsync>g__Awaited|0_0(Task task, TagHelperExecutionContext executionContext, int i, int count)
AspNetCore.Areas_Customer_Views_Basket_Index.ExecuteAsync() in Index.cshtml
+
{
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderAsync(ViewContext context)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ViewContext viewContext, string contentType, Nullable<int> statusCode)
Microsoft.AspNetCore.Mvc.ViewFeatures.ViewExecutor.ExecuteAsync(ActionContext actionContext, IView view, ViewDataDictionary viewData, ITempDataDictionary tempData, string contentType, Nullable<int> statusCode)

【问题讨论】:

  • 您是否明确允许延迟加载?如果没有,您是否在存储库查询中使用了“包含”语句?

标签: .net-core entity-framework-core


【解决方案1】:

你试过Include吗?您可以使用 Include 方法指定要包含在查询结果中的相关数据。

var basketLine = db.BasketLines
        .Include(b => b.Product)
        .FirstOrDefault(b => b.BasketId == BasketId && b.ProductId == productId);

[Source]

【讨论】:

    猜你喜欢
    • 2018-11-07
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多