我正在使用Entity Framework Core,需要指定ProductGraphic实体所属的产品,但是我得到了这个例外:
发生System.Data.SqlClient.SqlException HResult = 0x80131904
Message =引入FOREIGN KEY约束 ' FK_ShopProductGraphic_ShopProduct_ProductId'桌子上 ' ShopProductGraphic'可能会导致循环或多个级联路径。 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他 FOREIGN KEY约束。 。 。
如果我将ProductId类型设置为nullable,则数据库的init可以正常工作,但Product中的ProductGraphic列表始终为空。
ProductGraphic.cs :
[Table("ShopProductGraphic")]
public class ProductGraphic : Base
{
public int ProductId { get; set; }
public int GraphicId { get; set; }
public virtual Graphic Graphic { get; set; }
}
Product.cs:
[Table("ShopProduct")]
public class Product : BaseShop
{
public bool Active { get; set; } = true;
public int CategoryId { get; set; }
public int CultureId { get; set; } = -1;
public List<ProductInfo> InfoItems { get; set; } = new List<ProductInfo>();
public List<ProductGraphic> GraphicItems { get; set; }
}
Base.cs :
public abstract class Base
{
public int Id { get; set; }
public Guid UId { get; set; } = Guid.NewGuid();
public DateTime DateCreated { get; set; } = DateTime.Now;
}
ShopBase目前只是一个继承Base
的空抽象类答案 0 :(得分:1)
在ProductGraphic类中添加产品参考,就像你为Graphic做的那样,
[Table("ShopProductGraphic")]
public class ProductGraphic : Base
{
public int ProductId { get; set; }
public int GraphicId { get; set; }
public virtual Graphic Graphic { get; set; }
public virtual Product Product {get; set;}
}