EntityFramework error or does not save changes

0

I am having problems saving an entity in my EF6 context, I have tried it in several ways and in 1 it does not give an error but it does not save, the other gives an error.

var RegistroProducto = new tblProductos()
        {
            Estado = true,
            FechaInsercion = FechaRegistro,
            SKU = NoIdentificacion,
            Referencia = Referencia,
        };

        Tienda.tblProductos.Add(RegistroProducto);
        dbContext.SaveChanges();

This code does not give an error but does not save any changes in the db.

var RegistroProducto = new tblProductos()
        {
            tblTienda = Tienda,
            Estado = true,
            FechaInsercion = FechaRegistro,
            SKU = NoIdentificacion,
            Referencia = Referencia,               
        };

        dbContext.tblProductos.Add(RegistroProducto);
        dbContext.SaveChanges();

This code gives the following error: "An entity object can not be referenced by multiple instances of IEntityChangeTracker."

Any ideas?

    
asked by Archer_A 24.08.2016 в 23:46
source

2 answers

1
  

An entity object can not be referenced by multiple instances of IEntityChangeTracker.

This error indicates that you have a problem with the management of your contexts. More precisely, it means that you have an entity that is still "attached" to a context, but now you are trying to use that same entity with a different context.

Sadly, you have not shared enough code to really be a minimal, complete and verifiable example . However, according to what I can see, it is very likely that the culprit is the entity Tienda .

The entity Tienda must still be "attached" or linked to a context different from the context dbContext , so you can not use it with dbContext .

To illustrate the problem, try this simplified code (adjust it to the true names of your classes):

// cambia "TuDbContext" al nombre de tu DbContext.
using (var ctx1 = new TuDbContext())
{
    // Reemplaza esta sentencia con la que usas para cargar la instancia "Tienda".
    var Tienda = ctx1.tblTiendas.Where(t => t.Id == 123).Single();

    using(var ctx2 = new TuDbContext())
    {
        var RegistroProducto = new tblProductos()
        {
            tblTienda = Tienda,
            Estado = true,
            FechaInsercion = FechaRegistro,
            SKU = NoIdentificacion,
            Referencia = Referencia,              
        };

        ctx2.tblProductos.Add(RegistroProducto); // ¡ERROR!
    }
}

If you execute this example, you will see that it will result in the same error of yours in the following statement:

ctx2.tblProductos.Add(RegistroProducto);

Why? Because it is trying to add Tienda to context ctx2 , but Tienda still belongs to context ctx1 .

I'm not sure which is the best solution for your situation, because I can not see your complete code. But it seems very strange to me that you are handling multiple instances of your context simultaneously.

The solution may simply be to make sure that the context that loaded variable Tienda is closed for when you use the variable to add the new product. Or maybe it's a matter of making sure you're always using the same context. But at least, if you understand the cause, you will surely find the best way to correct the problem.

    
answered by 24.11.2016 в 03:46
0

You could do something like this:

public void InsertOrUpdate(Proveedor entity)
    {
        using (PosContext Context = new PosContext())
        {

                    Context.Proveedores.Add(entity);
                    Context.SaveChanges();
        }
    }
    
answered by 25.08.2016 в 04:11