0
I have the following scenario that I’m in trouble:
Category:
public class Categoria
{
public int Id { get; set; }
public string Descricao { get; set; }
public virtual ICollection<Produto> Produtos { get; set; }
}
Product:
public class Produto
{
public int Id { get; set; }
public string Descricao { get; set; }
public string Detalhes { get; set; }
public double Preco { get; set; }
public bool Disponivel { get; set; }
public int CategoriaId { get; set; }
public virtual Categoria Categoria { get; set; }
public virtual ICollection<Cliente> Clientes { get; set; }
}
Settings with Fluent Api:
public class CategoriaConfig : EntityTypeConfiguration<Categoria>
{
public CategoriaConfig()
{
ToTable("Categoria");
HasKey(x => x.Id);
Property(x => x.Descricao).IsRequired().HasMaxLength(100);
HasMany(x => x.Produtos);
}
}
public class ProdutoConfig : EntityTypeConfiguration<Produto>
{
public ProdutoConfig()
{
ToTable("Produto");
HasKey(x => x.Id);
Property(x => x.Descricao).IsRequired().HasMaxLength(100);
Property(x => x.Detalhes).IsRequired().HasMaxLength(100);
Property(x => x.Preco).IsRequired();
HasMany(x => x.Clientes);
HasRequired(x => x.Categoria);
}
}
Method to add the product (where the error is generated):
public void Adicionar(Produto produto)
{
_db.Entry(produto.Categoria).State = EntityState.Unchanged;
_db.Set<Produto>().Add(produto);
_db.SaveChanges();
}
The way the object is being passed to the Add method:
My Action:
public ActionResult Create(ProdutoViewModel produto)
{
if (ModelState.IsValid)
{
var produtoDomain = MapearParaDomainModel(produto);
produtoDomain.Categoria = _categoriaApp.ObterPorId(produto.CategoriaId);
_produtoApp.Adicionar(produtoDomain);
return RedirectToAction("Index");
}
return null;
}
Error message:
An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
As I am adding the product no need to search the category to put in the object to then record, so I removed the line "product Omain.Category = ..." from the action and also removed the line "_db. Entry(product.Category)..." of the write method, the error continues but its type is not specified.
Thanks Jhon! You said you have to make the "Unchanged" for each Product Instance Category, but the product only has one category. I also disabled Lazy Loading but it didn’t work either (I guess it didn’t influence the recording only in the data search, right?).
– Raphael
If you are saving only the product you would not also need to mark the category as unchanged.
– L. Jhon
Lazyloading also works to keep a record if you search the database to find out if it is to update or add... it will not load the aggregated objects and will not have to user the "Unchanged" for each charged instance.
– L. Jhon
Another check we can do is, before saving (apply Savechanges) is to look at the objects that are mapped in the Entity for database changes. It would be in _db.Product.Local (with the debug to navigate these levels and check if there is only that entity). If you have more than one object mapped to the same application instance, I recommend using Asnotraking in queries. Sorry for the delay in returning.
– L. Jhon