Many Deletion for Many Entity Framework

1

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.

    
asked by anonymous 02.02.2017 / 01:43

1 answer

2

There is a small conceptual error here, which becomes a significant error when records persist. When you do:

Orcamento.OrcamentoProfissionais.Remove(objParaRemoção) 

You are just deleting the object from the OrcamentoProfissionais list, not the database itself. To delete the database, you need to do the following:

contexto.OrcamentoProfissionais.Remove(objParaRemocao);

Then you will have the removal in context, that is, the Entity Framework will do all the heavy lifting for you.

In your case, the error occurs because you have highlighted the object in the budgets list, but it continues to exist in context and to be observed with it. What the context understands is that you left OrcamentoId null (after all, the object was removed from the list) and tries to save the record with the null key. This is what characterizes errors with this message.

In my view, the Entity Framework should be smarter and detect the removal of the way you did, but not all conceptual scenarios have been implemented in the framework .

    
02.02.2017 / 06:07