【发布时间】:2022-10-17 22:42:23
【问题描述】:
我正在尝试运行迁移以创建两个模型 Cart 和 CartItem:
public class Cart
{
[Key]
public string CartId { get; set; }
public List<CartItem> CartItems { get; set; }
}
public class CartItem
{
[Key]
public string CartItemId { get; set; }
public Product Product { get; set; }
[ForeignKey("Cart")]
public string CartRefId { get; set; }
public Cart CartId { get; set; }
}
在运行Add-Migration 命令时,我收到以下错误:
无法从实体类型“ShopIt.Models.CartItem (Dictionary<string, object>)”中删除属性“CartId”,因为它在“ShopIt.Models.CartItem (Dictionary<字符串,对象>)'。在删除属性之前,必须删除或重新定义所有包含的外键。
【问题讨论】:
-
删除
ForeignKey属性(不正确),将stringFK属性命名为CartId和导航属性Cart,一切都会好起来的。即public string CartId { get; set; } public Cart Cart { get; set; }
标签: c# .net-core entity-framework-core