首页 > 解决方案 > 在 EF6 Code-First 中管理 3 层表之间的级联删除

问题描述

我正在使用 EF6,我有下表。

  1. 产品
  2. 产品变体
  3. 产品展示

现在每个产品将有多个产品变体,每个产品变体又将有多个产品库(图像)。

以下是我设计模型的方式。

  1. 产品表
public class ProductsDo
{
    [Key]
    public int Id { get; set; }
    ... With all other fields pertaining to product.

    public virtual List<ProductVariantsDo> ProductVariants { get; set; }
}
  1. 产品变体表
public class ProductVariantsDo
{
    [Key]
    public int Id { get; set; }
    [ForeignKey("Product")]
    public int ProductId { get; set; }
    ... with all other fields pertaining to productvariants.

    public virtual ProductsDo Product { get; set; }
    public virtual List<ProductGalleryDo> ProductGalleries { get; set; }
}
  1. ProductGallery 表。
public class ProductGalleryDo
{
        [Key]
        public int Id { get; set; }
        [ForeignKey("ProductVariant")]
        public int ProductVariantId { get; set; }
        ... with all other fields pertaining to productgallery
        
        public virtual ProductVariantsDo ProductVariant { get; set; }

}

当我运行 update-database 命令时,它使用 ProductVariant 外键在 ProductGallery 上引发错误,它要求删除级联删除。但是在这里我担心如果我删除级联删除它不会在删除相应的产品变体时删除所有产品库。

谁能告诉我如何管理上述情况。

标签: c#asp.netsql-serverentity-frameworkforeign-keys

解决方案


推荐阅读