gpt4 book ai didi

c# - Entity Framework 代码优先要求的外键

转载 作者:太空宇宙 更新时间:2023-11-03 16:07:09 24 4
gpt4 key购买 nike

我正在为一个电子商务项目首先使用代码,

我有 2 个类:类别和产品

关系是一对多,Category有很多产品,我想根据需要制作外键(不为空),因此,如果我添加产品,我必须输入 categoryid。

当我这样做时,我得到这个错误:

Introducing FOREIGN KEY constraint 'FK_dbo.Products_dbo.Categories_categoryId' on table 'Products' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

有什么想法吗?

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; }
}

最佳答案

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

关于c# - Entity Framework 代码优先要求的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18903016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com