Foreign key Entity framework Asp.net mvc

Asked

Viewed 950 times

0

I have My Repair and Repair Table and I’m getting the following msg when I try to update my Migration: "The introduction of the FOREIGN KEY restriction 'Fk_dbo.Fixeddbo.Pecas_pecasid' in the 'Fix Settings' table can cause cycles or several cascading paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY restrictions. Could not create constraint or index. See previous errors."

    public class Conserto
{
    [Key]
    public int Id { get; set; }

    public DateTime DataEntrada { get; set; }
    public string NomeCliente { get; set; }
    public string NomeFuncionario { get; set; }
    public string NomePeca {get;set;}
    public string DescricaoDefeito { get; set; }
    public string DescricaoSolucao { get; set; }
    public decimal ValorMaoObra { get; set; }
    public decimal ValorTotalConserto { get; set; }
    public int PecasId { get; set; }
    public int ClienteId { get; set; }
    public int FuncionarioId { get; set; }


    [ForeignKey("PecasId")]
    public virtual Pecas Pecas { get; set; }

    [ForeignKey("ClienteId")]
    public virtual Cliente Cliente { get; set; }

    [ForeignKey("FuncionarioId")]
    public virtual Funcionario Funcionario { get; set; }

}

Conserdetalhes

    public class ConsertoDetalhes
{
    [Key]
    public int Id { get; set; }

    public int ConsertoId { get; set; }
    public int PecasId { get; set; }
    public decimal Total { get; set; }


    [ForeignKey("ConsertoId")]
    public virtual Conserto  Conserto { get; set; }

    [ForeignKey("PecasId")]
    public virtual Pecas Pecas { get; set; }
}
  • You put this model together yourself ? you really need the ( public int Pecasid { get; set; } ... public int Clienteid { get; set; } ... public int Funcionarioid { get; set; } ... and create public virtual Pecas Pecas { get; set; } ?

  • yes because when I’m creating, I need to have access to parts list, employee and customer.

1 answer

1


You are placing Foreignkey "Pecasid" in the two classes (which are already related)so Migration is complaining of circular reference, remove from one of the classes preferably of the class Fix.

  • The problem that if I remove from the repair I will not have access to part list.

  • By detailing the class Repair you can not? Since there is a relationship between the two.

  • When I am in View Create service order I need to have access to part list, client list and employee list. In Repair Parts I also need access to the Parts ID list and the Repair ID, so I need FK on both to get access to what I need

Browser other questions tagged

You are not signed in. Login or sign up in order to post.