3

我首先为电子商务项目使用代码,

我有 2 个课程:类别和产品

关系是一对多,Category有很多产品,我想按要求做外键(不为空)所以,如果我添加一个产品,我必须输入categoryid。

当我这样做时,我收到此错误:

在表“Products”上引入 FOREIGN KEY 约束“FK_dbo.Products_dbo.Categories_categoryId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。

有任何想法吗?

public class Category :IObjectWithState
{
    [Key]
    public int CategoryId { get; set; }
    [Required]
    public string Discription { get; set; }
    public string Notes { get; set; }
    public int ParentCategoryId { get; set; }

    public virtual ICollection<Product> Products { get; set; }
    public virtual ICollection<Discount> Discounts { get; set; }
    public virtual ICollection<ProductList> ProductLists { get; set; }

    [NotMapped]
    public State state { get; set; }
}

public class Product :IObjectWithState
{
    [Key]
    public int ProductId { get; set; }
    [Required]
    public string ProductName { get; set; }
    [Required]
    public string ShortDiscription { get; set; }
    public string LongDiscription { get; set; }

     [Required]
    public bool Active { get; set; }

    [Required]
    public int categoryId { get; set; }
    [ForeignKey("categoryId")]
    public  Category Category { get; set; }

    public ICollection<ProductImage> ProductImage { get; set; }
    public ICollection<Discount> Discount { get; set; }
    public ICollection<Discussion> Duscussion { get; set; }
    public ICollection<ProductAttributeValue> ProductAttributeValue { get; set; }
    public ICollection<ProductListItem> ProductListItem { get; set; }
    public ICollection<ProductSKU> ProductSKU { get; set; }
    public ICollection<RelatedProduct> RelatedProduct { get; set; }
    public ICollection<Review> Review { get; set; }
    public ICollection<ShoppingCart> ShoppingCart { get; set; }
}
4

1 回答 1

0

我在 Migration 类中将 cascadeDelete: 设置为 false

于 2013-09-21T14:07:48.540 回答