Many Exclusion for Many Entity Framework

Asked

Viewed 111 times

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.

1 answer

2


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

Orcamento.OrcamentoProfissionais.Remove(objParaRemoção) 

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

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 highlighted the object from the list of budgets, but it still exists in the context and is observed with it. What the context understands is that you left OrcamentoId null (after all, the object has been 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.

  • 1

    Thanks for the great explanation! I made some adjustments in my code and managed to delete as you said. Problem solved! o/

Browser other questions tagged

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