0
votes

i have many classes but in PorductionLine and Machine I have some problems. ProductionLine Class is:

[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. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

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!!

1

1 Answers

1
votes

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.

You can set cascadeDelete to false or true (in your migration Up() method). Depends upon your requirement.

AddForeignKey(..., cascadeDelete: false);

for more information check this question