I'm having a big problem with a many-to-many deletion in Entity Framework 6. Below is 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 are 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.