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?