1
I’m having trouble at the time of updating with relationship Many to Many.
I want the update method to update all itemEntity fields as well as vendors.
For example I only put the description field and suppliers according to the screen image.
In my code below if I remove the line that gives the error (shown below) it writes only suppliers.
I tried to set itemEntity as modified but I get error.
Can someone please help me?
My Canvas
Error
Attaching an Entity of type 'Domain.Entities.Itementity' 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.
Table
Relationship
Code
public override ItemEntity Atualizar(ItemEntity entity)
{
var novos = new List<FornecedorEntity>(entity.Fornecedores);
entity.Fornecedores = _dbContext.TbItem
.Where(p => p.Id == entity.Id).FirstOrDefault().Fornecedores;
_dbContext.Entry(entity).State = EntityState.Modified;
//Sem a linha acima ele funciona mais não atualiza as demais propriedades do ItemEntity,
//atualiza somente os fornecedores.
var deletetadoFornecedores = entity.Fornecedores
.Except(novos).ToList();
deletetadoFornecedores.ForEach(c => entity.Fornecedores.Remove(c));
var AdicionadoFornecedores = novos
.Except(entity.Fornecedores).ToList();
foreach (FornecedorEntity c in AdicionadoFornecedores)
{
if (_dbContext.Entry(c).State == EntityState.Detached)
{
_dbContext.TbFornecedor.Attach(c);
}
entity.Fornecedores.Add(c);
}
_dbContext.SaveChanges();
return entity;
}
Edited
This code does exactly what I need, but I have to keep mapping the properties.
public override ItemEntity Atualizar(ItemEntity entity)
{
var itemExistente = _dbContext.TbItem
.Find(entity.Id);
itemExistente.Descricao = entity.Descricao;
itemExistente.Descricao1 = entity.Descricao1;
itemExistente.Descricao2 = entity.Descricao2;
itemExistente.Descricao3 = entity.Descricao3;
itemExistente.Descricao4 = entity.Descricao4;
//Esse mapeamento acima não tem como fazer de forma automatica?
//Toda vez que eu criar uma nova propriedade vou ter que lembrar de vir aqui e alterar
//isso gera maior dificuldade na manutenão e aumenta a chance de esquecer e gerar um bug.
var fornecedoresDeletados = itemExistente.Fornecedores
.Except(entity.Fornecedores).ToList();
var fornecedoresAdicionados = entity.Fornecedores
.Except(itemExistente.Fornecedores).ToList();
fornecedoresDeletados.ForEach(c => itemExistente.Fornecedores.Remove(c));
foreach (FornecedorEntity c in fornecedoresAdicionados)
{
if (_dbContext.Entry(c).State == EntityState.Detached)
_dbContext.TbFornecedor.Attach(c);
itemExistente.Fornecedores.Add(c);
}
_dbContext.SaveChanges();
return entity;
}
Mauricio, in place of the error image could by the message?
– George Wurthmann
I can tell you what message?
– Mauricio Ferraz
I don’t know kkkk, I just can’t see the images here :(. It’s not a message you have in the Bug image?
– George Wurthmann
rsrs, Give it a click that it gets big. I put on it anyway.
– Mauricio Ferraz
No, that’s not it, the network I’m on is blocked. And it’s also important for the text, because if someone searches for the error it’s easier to get here. :)
– George Wurthmann
I think the only problem is not using the
EntityState.Modified
before saving the changes.– George Wurthmann
The problem is much bigger. I’m already working on something.
– Leonel Sanches da Silva
Opa vlw Gypsy, when I remove Entitystate.Modified it only updates suppliers.
– Mauricio Ferraz