1
I’m having a big problem with a many to many deletion in Entity Framework 6. Below follows my three classes.
Budget class.
public class Orcamento
{
public int Id { get; set; }
public int ClienteId { get; set; }
public virtual Cliente Cliente { get; set; }
public DateTime DataEvento { get; set; }
public int QtdPessoas { get; set; }
public int PorcentagemEntrada { get; set; }
public int PorcentagemFinal { get; set; }
public decimal ValorPessoa { get; set; }
public decimal ValorEntrada { get; set; }
public decimal ValorFinal { get; set; }
public decimal ValorTotal { get; set; }
public string Status { get; set; }
public string Obs { get; set; }
public bool EnviadoPorEmail { get; set; }
public DateTime DataRegistro { get; set; }
public virtual ICollection<Servico> ServicoLista { get; set; }
public virtual ICollection<OrcamentoProfissional> OrcamentoProfissionais { get; set; }
public Orcamento()
{
this.ServicoLista = new List<Servico>();
this.OrcamentoProfissionais = new List<OrcamentoProfissional>();
}
}
Professional class
public class Profissional
{
public int Id { get; set; }
public string Nome { get; set; }
public string Obs { get; set; }
public virtual ICollection<OrcamentoProfissional> OrcamentoProfissionais { get; set; }
public Profissional()
{
this.OrcamentoProfissionais = new List<OrcamentoProfissional>();
}
}
Class for relationship between the two classes above.
public class OrcamentoProfissional
{
public int Id { get; set; }
public int OrcamentoId { get; set; }
public int ProfissionalId { get; set; }
public int Quantidade { get; set; }
public virtual Orcamento Orcamento { get; set; }
public virtual Profissional Profissional { get; set; }
}
When I try to delete an object
Orcamento.OrcamentoProfissionais.Remove(objParaRemoção)
i get an exception:
The Operation failed: The Relationship could not be changed because one or more of the Foreign-key properties is non-nullable. When a change is made to a Relationship, the Related Foreign-key Property is set to a null value. If the Foreign-key does not support null values, a new Relationship must be defined, the Foreign-key Property must be Assigned Another non-null value, or the unrelated Object must be Deleted.
Thanks for the great explanation! I made some adjustments in my code and managed to delete as you said. Problem solved! o/
– Diego Augusto