ForeignKey 符合 EntityFramework Code First 中的需求
本文关键字:需求 First Code 符合 EntityFramework ForeignKey | 更新日期: 2023-09-27 18:31:57
我正在为电子商务项目首先使用代码,
我有 2 个类:类别和产品
关系是一对多,品类有很多产品,我想根据需要制作外键(不为空),因此,如果我添加产品,则必须输入类别 ID。
当我这样做时,我收到此错误:
引入外键约束 'FK_dbo.Products_dbo。表"产品"上的Categories_categoryId"可能会导致循环或多个级联路径。指定"删除时不执行任何操作"或"更新时不执行任何操作",或修改其他外键约束。
有什么想法吗?
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; }
}
我在迁移类中将 cascadeDelete: 设置为 false