简体   繁体   English

错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径-为什么?

[英]Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths - why?

i have many classes but in PorductionLine and Machine I have some problems. 我有很多课程,但是在PorductionLineMachine我有一些问题。 ProductionLine Class is: ProductionLine类为:

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

And Machine Class is: 机器类是:

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

when I want generate database form this classes I have this error: 当我想从此类生成数据库时,出现以下错误:

Introducing FOREIGN KEY constraint 'FK_dbo.TblMachine_dbo.TblProductionLine_FldProductionLineKey' on table 'TblMachine' may cause cycles or multiple cascade paths. 在表'TblMachine'上引入FOREIGN KEY约束'FK_dbo.TblMachine_dbo.TblProductionLine_FldProductionLineKey'可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。

Could not create constraint or index. 无法创建约束或索引。 See previous errors. 请参阅先前的错误。 when I comment this 3 lines 当我评论这三行

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

error is gone but I want this codes cus in some other classes I have this problem... what I have to do? 错误消失了,但是我想将此代码放在其他一些类中,但是我遇到了这个问题...我该怎么办? thanks for help!! 感谢帮助!!

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. 您收到此错误消息的原因是,在SQL Server中,表在由DELETE或UPDATE语句启动的所有级联引用动作的列表中不能出现多次。 For example, the tree of cascading referential actions must only have one path to a particular table on the cascading referential actions tree. 例如,级联引用动作树必须只有一个路径指向级联引用动作树上的特定表。

You can set cascadeDelete to false or true (in your migration Up() method). 您可以将cascadeDelete设置为false或true(在迁移Up()方法中)。 Depends upon your requirement. 取决于您的要求。

AddForeignKey(..., cascadeDelete: false);

for more information check this question 有关更多信息,请检查此问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL 错误:引入 FOREIGN KEY 约束可能会导致循环或多个级联路径。 实体框架核心 - SQL Error: Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Entity Framework Core 错误:引入FOREIGN KEY约束可能会导致循环或多个级联路径 - Error: introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths 在表table上引入FOREIGN KEY约束键可能会导致循环或多个级联路径。 指定ON DELETE…错误 - Introducing FOREIGN KEY constraint key on table table may cause cycles or multiple cascade paths. Specify ON DELETE … Error 当不需要属性时,引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign Key Constraint may cause cycles or multiple cascade paths when property is not required ef核心2-在表'Y'上引入FOREIGN KEY约束'X'可能会导致循环或多个级联路径 - ef core 2 - Introducing FOREIGN KEY constraint 'X' on table 'Y' may cause cycles or multiple cascade paths 在表上引入外键约束可能会导致循环或多个级联路径 - Introducing Foreign key Constraint on table may cause cycles or multiple cascade paths 在表“模型”上引入FOREIGN KEY约束“列”可能会导致循环或多个级联路径 - Introducing FOREIGN KEY constraint 'Column' on table 'Model' may cause cycles or multiple cascade paths 实体框架:在表 '' 上引入 FOREIGN KEY 约束 '' 可能会导致循环或多个级联路径 - Entity Framework: Introducing FOREIGN KEY constraint '' on table '' may cause cycles or multiple cascade paths 引入FOREIGN KEY约束可能会导致EF Core中的循环或多个级联路径 - Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths in EF Core 避免“引入FOREIGN KEY约束可能会导致循环或多个级联路径” - Avoiding 'Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM