Transaction with Entity Framework. Saves 2 times after error

Asked

Viewed 51 times

0

I am using transaction to make changes to the bank. My problem is when there is an error in saving time.

If there is error at the time of inserting the drive it treats the correct way not saving anything in the bank. However, when it comes back to the screen, I fix the problem (which was the size of the field at the base) and insert it again, only it records the database records twice.

inserir a descrição da imagem aqui

    public override void Add(NotaFiscalItem obj)
    {
        using (var transaction = Db.Database.BeginTransaction())
        {
            try
            {
                ItemMovimentacao mov = new ItemMovimentacao();
                mov.Numero_Item = obj.Numero_Item;
                mov.Origem_ItemMovimentacao = "Inclusão de Item de NF";

                Db.NotaFiscalItens.Add(obj);
                Db.ItemMovimentacoes.Add(mov);

                Db.SaveChanges();

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
        }
    }

That is, it generates the expected error for the overflow problem in the bank, but, when it goes back to the screen I have run again (already with the problem solved), it records again what he could not record the first time.

  • Why to use repositories?

  • I’m using a project following DDD. All uncoupled. So there’s the repository layer, service, domain.

  • Porém, quando eu volto, arrumo e executo novamente this threshing?

  • Yes. I actually only change the size of the field in the table to cause the error. Then I go back to normal size and run again.

  • That’s why it doesn’t work. See these answers. I don’t know who told you to use it like that, but it’s tremendously wrong.

  • Gypsy. Which part is wrong? I use the repository only to configure and perform basic EF operations.

  • It’s wrong. EF is a repository. You haven’t read anything I’ve pasted here?

  • I read Yes. But are we talking about a technical problem right? What you’re suggesting is a change in the implementation of the concept. Until then we would have to discuss a little more, as for example, the whole configuration of the bank, other than in the repository, would be in the domain? I think you’re right, I need to absorb some more. But when to the technical problem, in theory, it makes no difference whether it is in N layers or not certain?

  • Just one question. I don’t see where the answer to the informed question as duplicate answers mine. I have no problem duplicating the context. Probably the other way around. I even tried to do it with transaction in direct context, without using Transactionscope and the result is the same. It seems to store the error and save 2 times when there is no error. I will edit the question including with this information

  • It does make a difference, and I still think you didn’t read what I put in it for you. EF doesn’t admit "layered separation". What you did is use two contexts (in two different repositories), creating the problem of the highlighted context, which is the same thing as the question I marked there. Stop reinventing the wheel. Use the context within the Controller without separating into repositories.

  • If you edit your question and put only the second part (with the edition), I can answer. Then I put for reopening.

  • Amended question. Thank you.

Show 8 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.