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

标签 c# asp.net-core entity-framework-6 entity-framework-migrations

我正在尝试为电子商务解决方案编写支付流程代码,我想收集购物车中的所有产品,但随后在 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 中的列表甚至不显示在数据库设计中。

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

最佳答案

更改您的 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; }
}

关于c# - 如何使用 Entity Framework 6 在字段中存储对象列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55269485/

相关文章:

c# - 如何在 Python 中进行 groupby 列表理解(从 C#/LINQ 角度来看)

c# - 在部分 View 中存储 session

c# - 在 Visual Studio 中启用 TagHelper Intellisense 支持

asp.net-core - 对 nginx 反向代理背后的应用程序进行故障排除,因为 POST/PUT 请求回复错误 400(错误请求)

c# - EF 5-6.1.1 中的 NullReferenceException 具有两个相同类型的导航属性

c# - 在源中使用 NotMapped/Compulated 属性映射到目标时,Automapper ProjectTo<>() 出现问题

c# - 查明一个类型是否实现了一个泛型接口(interface)

C# API POST 请求

c# - 如何获取 Blob 存储中容器中的所有文件夹的列表?

html - 如果记录长度比列宽太长以及如何使所有列保持相同大小,如何放置点(...)