Upgrade from many to many with EF 6

Asked

Viewed 102 times

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 diagram State to Modified it gives error:

System.Invalidoperationexception: 'Attaching an Entity of type 'Acao' 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 yet 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 happening because I have an Action in 2 different transitions. But I’m only changing the Diagram.

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

inserir a descrição da imagem aqui

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

Someone has a light?

  • What is the complete code of logic manipulating transitions and actions?

  • Hello @Ciganomorrisonmendez, actually the records already exist and are already linked correctly (I’m not saving the transition updating the diagram, I do it direct), the problem is when I try to update any property of the diagram.

  • Just to clarify, the relations between diagram x transitions x actions have already come from the bank ready. I am only changing the description of the diagram. I don’t know if that’s what I wanted to know

  • That one obj is a Diagrama, right?

  • That’s right. I asked the question a few more things, the data with are in the database.

1 answer

2


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

If you’re just altering an object Diagrama, no reason to send the object Diagrama with all aggregated records. You send only the object and that’s it.

This suggests that you are using generic repository on top of Entity Framework, which I’ve explained hundreds of times that it’s wrong to use:

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, practice does not allow you to detach from observing the context entities that are not being manipulated (your case).

I don’t know if you’re using ASP.NET MVC. The easy way to solve this in ASP.NET MVC is in ASP.NET MVC Controller, excluding the properties of Binding:

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

If not, the simple way to solve it is by not setting the navigation property Transicoes:

diagrama.Transicoes = null;

Detach all objects from Transicao and Acao context also solves, but the operation takes longer because the Entity Framework tries to save not only the class object Diagrama, but all other aggregated objects:

foreach (var transicao in diagrama.Transicoes)
{
    Db.Entry(transicao).State = EntityState.Detached;
}
  • My case is Winforms. Setando null for solved transactions. I specialized the public override void update method Update(obj diagram) and includes this line. I’m not using generic repository, I just have a base class for simple registrations to use. I don’t know if that’s what you’re talking about. For the record, the second form does not resolve. Thank you very much.

  • 1

    The second way does not solve because probably another logic is reinserting the objects once again. My recommendation for Web Forms is to create and destroy contexts in Code Behind (like you’re already wearing: using (Contexto Db = new Contexto())). No need to define an intermediate layer unless you need to repeat the logic. If you need to repeat, create a Helper for every business rule that must be replicated, running code always in static scope. I’m always here to help ;)

  • 1

    Vlw Gypsy, I think I’m on the right track then, because that’s what I’m doing. I create and destroy in each request and only have a Base for the simplest Cruds. Thanks again.

Browser other questions tagged

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