【发布时间】:2019-03-07 12:21:20
【问题描述】:
我有一个购物车,可以从两个单独的桌子上接收商品。
一个表包含单个项目,另一个包含多个项目的框。
我正在使用 ApiController 将项目插入购物车,问题是当我插入 ID 为 1 的 Box 时,Cart 中的 FK 会将 ID 更新为 1,但没有指示它是 Item 还是 Box .
我尝试在购物车表中为每个项目和框 ID 设置多个 FK,但代码首先给出了有关 FK 中的空值的错误。我已尝试将它们设为可为空,但这会在尝试加入表以进行数据检索时导致错误。
下面显示的关系的最佳做法是什么?
购物车型号:
public class Cart
{
[Key]
public int RecordID { get; set; }
public string CartID { get; set; }
public int ItemID { get; set; }
public int BoxID { get; set; }
public int Qty { get; set; }
public System.DateTime DateCreated { get; set; }
public Item Item{ get; set; }
public Box Box { get; set; }
public string UserID { get; set; }
public ApplicationUser User { get; set; }
}
【问题讨论】:
标签: entity-framework model-view-controller ef-code-first-mapping