【问题标题】:Include in Entity Framework Core is not working properly包含在 Entity Framework Core 中无法正常工作
【发布时间】:2020-11-04 21:25:18
【问题描述】:

我正在尝试在用户购物篮中显示品牌名称、产品颜色和产品尺寸。我在这个项目中使用 ASP.NET Core 3.1。

我将此查询与include 一起使用:

public IEnumerable<UserBasket> GetUserBasket(int userId)
    {
        return _context.UserBaskets
            .Include(p=>p.Product)
            .Include(b=>b.ProductBrand)
            .Include(s=>s.ProductSize)
            .Include(c=>c.ProductColor)
            .Where(u=>u.UserId==userId)
            .ToList();
    }

这就是行动:

public IActionResult ShowOrders()
{
    var userId = _userService.GetUserIdByUserName(User.Identity.Name);
    return View(_orderService.GetUserBasket(userId));
}

这是动作的视图:

我用表格来展示用户购物篮

@model IEnumerable<UserBasket>

 @foreach (var item in Model)
                        {
                            <tr class="gradeA odd">
                                <td class="sorting_1">@item.Product.Name</td>
                                <td class="center ">@item.Count</td>
                                <td class="center ">@item.SumPrice</td>
                                <td class="center ">@item.CreateDate.Value.ToShamsi()</td>
                                <td class="center ">@item.ProductBrand.BrandName</td>
                                <td class="center ">@item.Size</td>
                                <td class="center ">@item.Color</td>
                                @if (item.IsFinally)
                                {
                                    <td class="center "><i class="glyphicon-ok"></i></td>
                                }
                                else
                                {
                                    <td class="center "><i class="glyphicon-remove"></i></td>
                                }
                            </tr>
                        }

这是 UserBasket 类:

public class UserBasket
{
    public UserBasket()
    {

    }
    [Key]
    public int UB_Id { get; set; }
    public int UserId { get; set; }
    public int ProductId { get; set; }
    public int PB_Id { get; set; }
    public int PC_Id { get; set; }
    public int PS_Id { get; set; }
    public int Count { get; set; }
    public int SumPrice { get; set; }
    public bool IsFinally { get; set; }
    public DateTime? CreateDate { get; set; }
    [Required]
    [MaxLength(200)]
    public string BrandName { get; set; }
    [Required]
    [MaxLength(200)]
    public string Color { get; set; }
    [Required]
    [MaxLength(200)]
    public string Size { get; set; }

    #region relations

    public virtual User.User User { get; set; }
    public virtual Product.Product Product { get; set; }
    public virtual ProductBrand ProductBrand { get; set; }
    public virtual ProductColor ProductColor { get; set; }
    public virtual ProductSize ProductSize { get; set; }

    #endregion
}

这是产品类:

public class Product
{
    public Product()
    {

    }
    [Key]
    public int ProductId { get; set; }
    public int CategoryId { get; set; }
    public int PB_Id { get; set; }
    public int PC_Id { get; set; }
    public int PS_Id { get; set; }
    public int Price { get; set; }
    public bool IsAvailable { get; set; }
    public string Name { get; set; }
    public string ShortDescription { get; set; }
    public string Description { get; set; }
    public string ImageName { get; set; }
    public DateTime? CreateDate { get; set; }
    public bool? IsDeleted { get; set; }
    public int? Visit { get; set; }

    #region relations

    public virtual Category Category { get; set; }
    public virtual ProductBrand ProductBrand { get; set; }
    public virtual ProductColor ProductColor { get; set; }
    public virtual ProductSize ProductSize { get; set; }
    public virtual List<UserBasket> UserBaskets { get; set; }

    #endregion
}

这是用户类:

 public class User
{
    public User()
    {
        
    }
    [Key]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string ActiveCode { get; set; }
    public bool IsActive { get; set; }
    public DateTime? RegisterDate { get; set; }
    public bool? IsDeleted { get; set; }

    #region relations
    public virtual List<UserBasket> UserBaskets { get; set; }
    public virtual List<UserOrder> UserOrders { get; set; }
    #endregion
}

当我运行它显示的项目时

System.NullReferenceException: '对象引用未设置为对象的实例。'

在视图中。我该怎么办?

【问题讨论】:

  • 你能告诉我们 UserBasket 类吗?
  • 我用 UserBasket 类编辑了问题。

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


【解决方案1】:

您的用户和产品之间的关系似乎是多对多的。

在我的项目中,我成功显示了多个产品名称。这是我的例子:

UserBacket 类:

public class UserBaskets
{
    public int Id { get; set; }
    public int SumPrice { get; set; }
    public DateTime CreateDate { get; set; }
    public bool IsFinally { get; set; } 
    public int UserId { get; set; }
    public int ProductId { get; set; }
    public Product Product { get; set; }
    public User User { get; set; }
}

产品类别:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<UserBaskets> UserBaskets{ get; set; }
}

用户:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<UserBaskets> UserBaskets { get; set; }
}

然后在你的视图中:

@model IEnumerable<UserBaskets>

@foreach (var item in Model)
{
    <tbody>
        <tr class="gradeA odd">
            <td class="sorting_1">@item.Product.Name</td>
            <td class=" ">@item.SumPrice</td>
            <td class="center ">@item.CreateDate</td>
            <td class="center ">@item.IsFinally</td>
        </tr>
    </tbody>
}

【讨论】:

  • 我试过了,但没用。它显示了我期望的所有记录,但不再显示产品名称。
  • 我做了和你一样的事情,但仍然没有工作,它只显示一条记录。是不是整个项目或者包有问题?
  • 首先,项目或包出现问题的可能性很小。你要做的就是确保你的关系是正确的,然后数据库中有多条数据。您可以与我们分享您的User, Product, UserBackets 表。我对您的代码"public virtual User.User User {get; set; }" 中的User.User 感到困惑。
  • 我用这些表更新了问题。第一个用户是命名空间。其实关系是这样的。 public virtual User User {get; set; } 我终于可以显示产品名称视图,但现在我无法在视图中显示 BrandName 和 productSize 和 ProductColor。所有的关系就像 product 和 userbasket。我不知道为什么它不起作用。
【解决方案2】:

请改变

@foreach (var item in Model)
{
    <tbody>
        <tr class="gradeA odd">
            <td class="sorting_1">@item.Product.Name</td>
            <td class=" ">@item.SumPrice</td>
            <td class="center ">@item.CreateDate</td>
            <td class="center ">@item.IsFinally</td>
        </tr>
    </tbody>
}

   <tbody>
@foreach (var item in Model){
 
        <tr class="gradeA odd">
            <td class="sorting_1">@item.Product.Name</td>
            <td class=" ">@item.SumPrice</td>
            <td class="center ">@item.CreateDate</td>
            <td class="center ">@item.IsFinally</td>
        </tr>
   
}
 </tbody>

我猜你每个购物篮可以有多个产品,所以从 UserBaskets 表中删除你的 ProductId

   [Table("UserBaskets")]
public class UserBasket
{
    public int Id { get; set; }
    public int SumPrice { get; set; }
    public DateTime CreateDate { get; set; }
    public bool IsFinally { get; set; } 
    public int UserId { get; set; }
     public User User { get; set; }
}

并创建新类 UserBasketProduct 并在其中添加产品

public class UserBasketProduct
{
public int UserBasketId { get; set; }
    public virtual UserBasket UserBasket{ get; set; }
 public int ProductId { get; set; }
    public virtual Product Product { get; set; }
}
.... add some properties to dbcontext

在此之后根据更新的 EF 进行查询

【讨论】:

  • 如果每个用户购物篮根据您的EF只有一个产品,或者每个购物篮可以有多个产品?
  • 关系是真实的。我测试了这些模型以及与另一个项目的关系。它工作正常,也显示了产品名称。是不是整个项目或者包有问题,影响查询?
  • 由于您将 productId 作为用户购物篮的外来密钥,因此只有当您的购物篮只能包含一种产品时,您的 EF 才能正常工作。这是真的吗?保持多对多的唯一方法是添加一个表。它可以像现在一样工作,但前提是您将所有内容都保留在内存中。
  • 或者如果您的购物篮没有主键并且您必须为该购物篮中的每个产品创建相同的购物篮,它会像这样工作。
  • 我觉得我的vs或者数据库有问题。我将整个项目复制到一个新项目中,它工作正常,但现在我有帐户激活问题,我在以前的项目中没有这个问题。
【解决方案3】:

现在一切正常。 我不应该在 userbasket 类中使用这些:

 [Required]
[MaxLength(200)]
public string BrandName { get; set; }
[Required]
[MaxLength(200)]
public string Color { get; set; }
[Required]
[MaxLength(200)]
public string Size { get; set; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2017-03-24
    • 2016-10-03
    • 2017-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多