【问题标题】:All containing foreign keys must be removed or redefined before the property can be removed必须先删除或重新定义所有包含的外键,然后才能删除该属性
【发布时间】:2022-10-17 22:42:23
【问题描述】:

我正在尝试运行迁移以创建两个模型 CartCartItem

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<字符串,对象>)'。在删除属性之前,必须删除或重新定义所有包含的外键。

【问题讨论】:

标签: c# .net-core entity-framework-core


【解决方案1】:

对我有用的是删除旧的迁移。 使用此命令删除迁移。它删除了以前的迁移。

Remove-Migration

【讨论】:

    猜你喜欢
    • 2021-05-16
    • 1970-01-01
    • 2021-10-27
    • 2019-10-24
    • 1970-01-01
    • 2017-08-10
    • 1970-01-01
    • 2022-12-21
    • 1970-01-01
    相关资源
    最近更新 更多