2016-09-18 64 views
0

我有很多課程,但在PorductionLineMachine我有一些問題。 ProductionLine類是:錯誤:引入FOREIGN KEY約束可能會導致循環或多個級聯路徑 - 爲什麼?

[Column("FldKeyId")] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Required] 
    [Key] 
    public int MyKeyId { get; set; } 
    [Column("FldCode")] 
    [Required] 
    [Index(IsUnique = true)] 
    public int MyCode 
    { 
     get { return _Code; } 
     set { _Code = value; } 
    } 
    [Column("FldName")] 
    [Required] 
    public string MyName 
    { 
     get { return _Name; } 
     set { _Name = value; } 
    } 
    [Column("FldLocation")] 
    [Required] 
    public string MyLocation 
    { 
     get { return _Location; } 
     set { _Location = value; } 
    } 

    [Column("FldCompanyKey")] 
    public int MyCompanyKey { get; set; } 
    [ForeignKey("MyCompanyKey")] 
    [Required] 
    public virtual Company Company { get; set; } 

和機器類是:

[Column("FldKeyId")] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    [Required] 
    [Key] 
    public int MyKeyId { get; set; } 
    [Column("FldCode")] 
    [Required] 
    [Index(IsUnique = true)] 
    public int MyMachineCode 
    { 
     get { return _MachineCode; } 
     set { _MachineCode = value; } 
    } 
    [Column("FldName")] 
    [Required] 
    public string MyName 
    { 
     get { return _Name; } 
     set { _Name = value; } 
    } 

    [Column("FldProductionLineKey")] 

    public int MyProductionLineKey { get; set; } 
    [ForeignKey("MyProductionLineKey")] 
    //[Required] 
    public ProductionLine ProductionLine { get; set; } 

時,我想生成數據庫表單這個班我有這樣的錯誤:

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

無法創建約束或索引。查看以前的錯誤。 當我評論這3條線

[Column("FldProductionLineKey")] 
public int MyProductionLineKey { get; set; } 
[ForeignKey("MyProductionLineKey")] 

錯誤是走了,但我想這個代碼CUS在一些其他類我有這個問題...... 我有什麼做的? 感謝您的幫助!

回答

1

You receive this error message because in SQL Server, a table cannot appear more than one time in a list of all the cascading referential actions that are started by either a DELETE or an UPDATE statement. For example, the tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree.

您可以將cascadeDelete設置爲false或true(在您的遷移Up()方法中)。取決於你的要求。

AddForeignKey(..., cascadeDelete: false); 

更多信息檢查this question

+0

確定一個時間,它的工作,但它的商業計劃,並在負荷消費系統我想'database.CreateIfNotExist'是屬性還是這樣,我把的東西箱子數據庫我的班級工作'cascadeDelete:false' – sadeq

+0

@sadeq你可以做到流利的地圖 –

+0

感謝它的好主意 – sadeq

相關問題