Context trying to delete 2 times the same record

0

I'm having a problem, in fact it should be the lack of knowledge even at the time of deleting a record using Entity Framework 6.

When I try to delete a record that can not be deleted because it has references, it gives the following error:

Sofar,allright.

I'lltrytodeleteanotherrecord,whichI'msurehasnoreference,anditgivesthesameerror,it'sactuallytryingtodeletethepreviousrecordagain.

IfIleavethescreenandenteragainthenIcandeletetheregistrynormally,aslongasIdonottrytodeleteanotheronebeforeitgeneratestheerror.

Doesanyonehavealight?Vlw

EDIT:Here'smycodetobetterunderstand:

ScreenSignature:

privatereadonlyICategoriaAppService_categoriaApp;privateCategoriaViewModel_categoria;publicFormCategorias(ICategoriaAppServicecategoriaAppService){_categoriaApp=categoriaAppService;InitializeComponent();}

Methodtoexclude:

privatevoidbtnExcluir_Click(objectsender,EventArgse){DialogResultresposta=MessageBox.Show("Deseja excluir este registro?", "Excluir", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    switch (resposta)
    {
        case DialogResult.Yes:
            var categoriaDomain = _categoriaApp.ObtemPeloID(_categoria.Numero_Categoria);
            _categoriaApp.Excluir(categoriaDomain);
            MessageBox.Show("Registro excluido com sucesso");
            break;
        case DialogResult.No:
            break;
    }
}

AppService:

private readonly ICategoriaService _categoriaService;

public CategoriaAppService(ICategoriaService categoriaService) : base(categoriaService)
{
    _categoriaService = categoriaService;
}

public void Excluir(TEntity obj)
{
    _serviceBase.Remove(obj);
}

Service:

public class ServiceBase<TEntity> : IDisposable, IServiceBase<TEntity> where TEntity : class
{
    protected ControleEstoqueContexto Db = new ControleEstoqueContexto();

    public ServiceBase()
    {
        Db.Configuration.AutoDetectChangesEnabled = false;
    }

    public virtual void Remove(TEntity obj)
    {
        try
        {
            Db.Set<TEntity>().Remove(obj);
            Db.SaveChanges();
        }
        catch (Exception e)
        {
            Db.Dispose();
            Db = new ControleEstoqueContexto();
            throw e;
        }
    }

    //ETC
}

EDIT 2: Resolved by putting try catch and remapping Db.Context. Code changed above.

    
asked by anonymous 15.12.2015 / 17:13

2 answers

1

Putting your code makes it easier to pinpoint the problem.

What seems to be happening to you is as follows.

Contexto.Objetos.Remove(instancia1);
Contexto.SaveChanges();

Error ... After the error you do:

Contexto.Objetos.Remove(instancia2);
Contexto.SaveChanges();

Since instancia2 has no key. But the context still has the "order" to exclude instancia1 . And it tries to exclude both instances. So, it makes an error in trying to delete the 1. Do you understand? After the error redo the context, or see the best logic for you (not in your code it is difficult to say what it would be) and test.

When you close the screen and open again the context is redone, so when you redo only Remove(instancia2) it works.

UPDATING

You do not load the form injects an instance of the context (pretty cool). During the execution of the events you will use the same context.

First thing I would do is treat the error, or make it delete the relationship or not let or get there. That's what I suggest.

If it is not possible, it is best to create new, but do not forget to make a Dispose() of the current one.

Here has another answer also suggesting new instead of changing the status of the object that is to be deleted .

    
15.12.2015 / 17:25
0

In its place, I would not inject form into a service, but a context. A Microsoft tutorial goes exactly on this line .

This is more of a problem in the highlighted context. You load the data using one context and try to persist using another context.

    
15.12.2015 / 19:02