Asp.net core Include in Entity Framework核心无法正常工作

Asp.net core Include in Entity Framework核心无法正常工作,asp.net-core,entity-framework-core,include,asp.net-core-3.1,Asp.net Core,Entity Framework Core,Include,Asp.net Core 3.1,我试图在用户篮中显示品牌名称、产品颜色和产品尺寸。我在这个项目中使用ASP.NETCore3.1 我将此查询与include一起使用: public IEnumerable<UserBasket> GetUserBasket(int userId) { return _context.UserBaskets .Include(p=>p.Product) .Include(b=>b.ProductBra

我试图在用户篮中显示品牌名称、产品颜色和产品尺寸。我在这个项目中使用ASP.NETCore3.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>
                        }
这是产品类别:

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 Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<UserBaskets> UserBaskets{ get; set; }
}
公共类产品
{
公共产品()
{
}
[关键]
public int ProductId{get;set;}
public int CategoryId{get;set;}
公共int PB_Id{get;set;}
公共int PC_Id{get;set;}
公共int PS_Id{get;set;}
公共整数价格{get;set;}
公共布尔值可用{get;set;}
公共字符串名称{get;set;}
公共字符串ShortDescription{get;set;}
公共字符串说明{get;set;}
公共字符串ImageName{get;set;}
公共日期时间?CreateDate{get;set;}
公共布尔?被删除{get;set;}
公共int?访问{get;set;}
#区域关系
公共虚拟类别{get;set;}
公共虚拟产品品牌{get;set;}
公共虚拟ProductColor ProductColor{get;set;}
公共虚拟ProductSize ProductSize{get;set;}
公共虚拟列表UserBaskets{get;set;}
#端区
}
这是用户类:

 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
}
公共类用户
{
公共用户()
{
}
[关键]
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
公共字符串ActiveCode{get;set;}
公共bool IsActive{get;set;}
公共日期时间?RegisterDate{get;set;}
公共布尔?被删除{get;set;}
#区域关系
公共虚拟列表UserBaskets{get;set;}
公共虚拟列表UserOrders{get;set;}
#端区
}
当我运行项目时,它会显示

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

在视野中。我该怎么办?

请换衣服

@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>
}
并创建新类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进行查询,用户和产品之间的关系似乎是多对多的

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

UserBacket类:

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 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; }
}
 [Required]
[MaxLength(200)]
public string BrandName { get; set; }
[Required]
[MaxLength(200)]
public string Color { get; set; }
[Required]
[MaxLength(200)]
public string Size { get; set; }
产品类别:

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 Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<UserBaskets> UserBaskets{ get; set; }
}
公共类产品
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表UserBaskets{get;set;}
}
用户:

公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共列表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>
}
@model IEnumerable
@foreach(模型中的var项目)
{
@item.Product.Name
@项目.总价
@item.CreateDate
@item.IsFinally
}
现在工作正常。 我不应该在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 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; }
}
 [Required]
[MaxLength(200)]
public string BrandName { get; set; }
[Required]
[MaxLength(200)]
public string Color { get; set; }
[Required]
[MaxLength(200)]
public string Size { get; set; }

你能给我们看看UserBasket类吗?我用UserBasket类编辑了这个问题。我试过了,但没有成功。它显示了我期望的所有记录,但不再显示产品名称。我做了与您完全相同的事情,但仍然不工作,它只显示一条记录。整个项目或包是否可能有问题?首先,项目或包出现问题的可能性很小。您需要做的是确保您的关系是正确的,然后数据库中有多个数据段。您可以与我们共享您的
用户、产品、用户备份表。我对您的代码“public virtual User.User{get;set;}”中的
User.User
感到困惑。我用这些表更新了问题。第一个用户是名称空间。其实关系是这样的<代码>公共虚拟用户用户{get;set;}
我终于可以显示product name视图了,但现在我无法在视图中显示BrandName、productSize和ProductColor。所有关系都类似于product和userbasket。我不知道为什么它不起作用。如果每个用户Buschket根据您的EF只有一个产品,或者每个篮子可以有几个productus?这些关系是正确的。我测试了这些模型以及与另一个项目的关系。它运行良好,并显示了产品名称。整个项目或包是否可能存在问题并影响查询?由于您的productId是用户篮的外国人密钥,因此只有当您的篮只能有一个产品时,EF才能正常工作。这是真的吗?保持多对多的唯一方法是增加一个表。它可以像你现在这样工作,但前提是你把所有的东西都保存在内存中。或者,如果你的篮子没有主键,你必须为这个篮子中的每个产品创建相同的母线。我认为我的vs或数据库有问题。我将整个项目复制到一个新项目中,它运行良好,但现在我有帐户激活问题,我在以前的项目中没有这个问题。