Many-to-Many Update with EF 6

1

My problem is when I try to update an entity that has a child class that in turn has other children. The structure is this:

public class Diagrama
{
    [Key]
    public int Numero_Diagrama { get; set; }

    public virtual List<Transicao> Transicoes { get; set; }
}

public class Transicao
{
    [Key]
    public int Numero_Transicao { get; set; }

    public int Numero_Diagrama { get; set; }

    public virtual List<Acao> Acoes { get; set; }
}

public class Acao
{
    [Key]
    public int Numero_Acao { get; set; }

    public virtual List<Transicao> Transicoes { get; set; }
}
  

Diagram for Transition (1-n)

     

Transition to Actions (n-n)

modelBuilder.Entity<Transicao>()
    .HasMany(s => s.Acoes)
    .WithMany(c => c.Transicoes)
    .Map(cs =>
    {
         cs.MapLeftKey("Numero_Transicao");
         cs.MapRightKey("Numero_Acao");
         cs.ToTable("wrfTransicaoAcoes");
    });

When I try to change the State of the diagram to Modified it gives the error:

  

System.InvalidOperationException: 'Attaching an entity of type' Action 'failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate. '

    public virtual void Atualizar(TEntity obj)
    {
        ValidaEntidade(obj);

        using (Contexto Db = new Contexto())
        {
            Db.Entry(obj).State = EntityState.Modified;
            Db.SaveChanges();
        }
    }

This is occurring because I have an Action in 2 different transitions. But I'm just changing the Diagram.

Data structure, already in the database and loaded into the class Diagram:

If I delete the last line of transition 49, everything works because it also references action 4.

Does anyone have a light?

    
asked by anonymous 13.03.2017 / 03:17

1 answer

2

The error means that you sent the same object Acao to the context twice, being that in the first object there is no change, and in the second there is change.

If you are just changing a Diagrama object, you do not have to send the Diagrama object to all records added. You just send the object and that's it.

This suggests that you are using generic repository over Entity Framework, which #:

public virtual void Atualizar(TEntity obj)
{
    ValidaEntidade(obj);

    using (Contexto Db = new Contexto())
    {
        Db.Entry(obj).State = EntityState.Modified;
        Db.SaveChanges();
    }
}

In addition to being wrong, the practice does not allow you to detach from the observation of context entities that are not being manipulated (your case).

I do not know if you are using ASP.NET MVC. The easy way to work around this in ASP.NET MVC is in Controller , excluding binding properties:

public ActionResult Editar([Bind(Exclude = "Transicoes")] Diagrama diagrama)
{ ... }

If not, the simple way to resolve this is by not setting the Transicoes :

diagrama.Transicoes = null;
Detaching all objects of Transicao and Acao of context also resolves, but the operation takes longer because the Entity Framework tries to save not only the object of class Diagrama , but all other objects added:

/ p>

foreach (var transicao in diagrama.Transicoes)
{
    Db.Entry(transicao).State = EntityState.Detached;
}
    
13.03.2017 / 16:03