C# 在init上构建数据库时获取外键的SqlException
我正在使用Entity Framework Core,需要指定ProductGraphic实体所属的产品,但我得到以下例外情况: System.Data.SqlClient.SqlException发生HResult=0x80131904C# 在init上构建数据库时获取外键的SqlException,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我正在使用Entity Framework Core,需要指定ProductGraphic实体所属的产品,但我得到以下例外情况: System.Data.SqlClient.SqlException发生HResult=0x80131904 Message=引入外键约束 表上的“FK_ShopProductGraphic_ShopProduct_ProductId” “ShopProductGraphic”可能导致循环或多个级联路径。 指定在删除时不执行操作或在更新时不执行操作,或修改其他 外键约
Message=引入外键约束 表上的“FK_ShopProductGraphic_ShopProduct_ProductId” “ShopProductGraphic”可能导致循环或多个级联路径。 指定在删除时不执行操作或在更新时不执行操作,或修改其他 外键约束 如果我将ProductId类型设置为null,则数据库的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; }
}
public abstract class Base
{
public int Id { get; set; }
public Guid UId { get; set; } = Guid.NewGuid();
public DateTime DateCreated { get; set; } = DateTime.Now;
}
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; }
}
ShopBase目前只是一个空的抽象类,它继承了Base在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;}
}
请张贴基地和基地类的快照了。ProductGraphics类中缺少产品引用,Product类中缺少ProductId。这些可能是在基类或BaseShop类中定义的,这就是为什么你也应该发布基类和BaseShop类的快照。我已经用基类的信息更新了这个问题,谢谢你指出这一点