如何使用Entity Framework 6在字段中存储对象列表

问题描述 投票:2回答:1

我正在尝试编写电子商务解决方案的付款流程,我想收集购物车中的所有产品,然后在List<OrderProducts>内部,然后将该列表存储到一个对象Order

POCO课程如下:

Order.cs

public class Order
{
    public int Id { get; set; }
    public double TotalAmount { get; set; }
    public IdentityUser UserAccount { get; set; }
    public virtual ICollection<OrderProduct> OrderProduct { get; set; }
    public DateTime OrderDate { get; set; }
    public bool IsComplete { get; set; }
}

OrderProduct.cs

public class OrderProduct
{
    public int Id { get; set; }
    public Order Order { get; set; }
    public Product Product { get; set; }
    public int Quantity { get; set; }
}

在运行迁移并更新数据库之后,OrderProduct表中的Order列似乎甚至没有显示在数据库设计中。

根据上述情况,有人可以帮助或提供更好的解决方案吗?

c# asp.net-core entity-framework-6 ef-migrations
1个回答
0
投票

更改OrderProduct类,然后重新生成迁移:

public class OrderProduct
{
    [Key]
    public int Id { get; set; }
    public int OrderId {get;set;}
    [ForeignKey(nameof(OrderId))]
    public virtual Order Order { get; set; }
    public Product Product { get; set; }
    public int Quantity { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.