2017-08-06 65 views
2

進出口使用實體框架的核心,需要指定產品的ProductGraphic實體所屬,但我得到這個異常:獲取SQLEXCEPTION的外鍵初始化建立數據庫時

System.Data.SqlClient.SqlException發生HResult = 0x80131904
消息=引入FOREIGN KEY約束 'FK_ShopProductGraphic_ShopProduct_ProductId' 'ShopProductGraphic'可能導致週期或多個級聯路徑。 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他的FOREIGN KEY約束。 。 。 。

如果我將ProductId類型設置爲可爲null,則數據庫的初始化工作正常,但產品中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目前只是一個空洞的抽象類,它繼承了ProductGraphic Base類

+0

請張貼基地和BaseShop類的快照了。您缺少ProductGraphics類中的Product Reference和Product類中的ProductId。這些可能在Base或BaseShop類中定義,這就是爲什麼你應該發佈Base,BaseShop類的快照。 – Dronacharya

+0

我已經更新了有關基類信息的問題,謝謝指出 – TheRuler

回答

1

添加產品參考喜歡你做的圖形,

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