Error in Insert Entity Framekwork - Mysql

Asked

Viewed 85 times

0

I am getting the following error while trying to perform Savechanges():

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

Code:

try
            {
                if (curriculumVitae.DataUltimaAtualizacao == null)
                {
                    curriculumVitae.DataUltimaAtualizacao = DateTime.ParseExact(
                        String.Format("{0} {1}", cvXml.DATAATUALIZACAO, cvXml.HORAATUALIZACAO),
                        Utils.ParseDateFormat(cvXml.FORMATODATAATUALIZACAO) + " %Hmmss",
                        null
                    );
                }

                var professor = this.ProcurarProfessorPorNumeroCurriculo(curriculumVitae.NumeroCurriculo);

                if (professor != null && professor.DataUltimaAtualizacaoCurriculo >= curriculumVitae.DataUltimaAtualizacao)
                {
                    Logger.Info(String.Format(
                        "Currículo {0} do Professor {1} já esta na última versão.",
                        curriculumVitae.NumeroCurriculo,
                        curriculumVitae.NomeProfessor
                    ));
                    return true;
                }

                ProcessarCache(cvXml);

                if (professor != null)
                {
                    RemoverDadosProfessor(professor);
                }
                else
                {
                    // criar o professor
                    professor = LattesDatabase.Professor.Create();
                    criarProfessor = true;
                }

                // dados do curriculo
                professor.NumeroCurriculo = curriculumVitae.NumeroCurriculo;
                professor.LinkParaCurriculo = String.Format("http://lattes.cnpq.br/{0}", professor.NumeroCurriculo);

                professor.DataUltimaAtualizacaoCurriculo = (DateTime)curriculumVitae.DataUltimaAtualizacao;

                professor.DataUltimaPublicacaoCurriculo = new DateTime(1900, 1, 1); // data qualquer no passado

                ProcessarDadosGerais(professor, cvXml);

                ProcessarEnderecosEContatos(professor, cvXml);

                ProcessarIdiomas(professor, cvXml);

                ProcessarPremiosETitulos(professor, cvXml);

                ProcessarFormacaoAcademicaETitulacao(professor, cvXml);

                ProcessarAreasAtuacaoProfessor(professor, cvXml);

                ProcessarProducoesTecnicasEPatentes(professor, cvXml);

                ProcessarProducoesBibliograficas(professor, cvXml);

                ProcessarParticipacaoEventos(professor, cvXml);

                ProcessarOrientacoesESupervisoes(professor, cvXml);

                ProcessarBancasDeTrabalho(professor, cvXml);

                ProcessarBancasJulgadoras(professor, cvXml);

                ProcessarAtuacoesProfissionaisEProjetos(professor, cvXml);

                if (criarProfessor)
                {
                    LattesDatabase.Professor.Add(professor);
                }
                LattesDatabase.SaveChanges(); 
                CriarBaseDeConsulta(professor);
                LattesDatabase.SaveChanges(); // salva base de consulta
                return true;
            }

Stack:

    2019-05-06 14:40:38,448 [5] ERROR ProfessorDAOService - Exceção: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
   em System.Data.Entity.Internal.InternalContext.SaveChanges()
   em System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
   em System.Data.Entity.DbContext.SaveChanges()
   em LattesExtractor.DAO.ProfessorDAOService.ProcessCurriculumVitaeXML(CurriculoVitaeXml cvXml, CurriculoEntry curriculumVitae) na C:\Users\Unisc\source\ExtratorLattesCNPq\LattesExtractor\DAO\ProfessorDAOService.cs:linha 257
2019-05-06 14:40:38,448 [5] ERROR ProfessorDAOService - Exceção Interna [1]: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.
   em System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.ValidateRowsAffected(Int64 rowsAffected, UpdateCommand source)
   em System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator.Update()
   em System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.<Update>b__2(UpdateTranslator ut)
   em System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update[T](T noChangesResult, Func`2 updateFunction)
   em System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update()
   em System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__35()
   em System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
   em System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions options, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction)
   em System.Data.Entity.Core.Objects.ObjectContext.<>c__DisplayClass2a.<SaveChangesInternal>b__27()
   em System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute[TResult](Func`1 operation)
   em System.Data.Entity.Core.Objects.ObjectContext.SaveChangesInternal(SaveOptions options, Boolean executeInExistingTransaction)
   em System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options)
   em System.Data.Entity.Internal.InternalContext.SaveChanges()

Some tables in the database are given insertions/updates, but those that are related to the Teacher table do not happen (if I enter manually in the Teacher table the id is defined as if they had records in the table, even if I have none)

  • Good afternoon, @Ander_. Just trying to understand, what is the need to write Lattesdatabase.Savechanges() 2x in the same block? If you save block changes: if (creatProfessor), you don’t need to, okay? Tries to invoke the Savechanges() method only at the end of the whole process.

1 answer

0

From what I could understand from your code, the database context instance is happening after the persistence command is executed. Also the persistence command is running twice. Ideal is that it is executed only once, this will make all the necessary execution of operation in the bank.

You notice that the database id has progressed because there has been an insertion attempt, this "consumes" an id even though the insertion is not successful.

One thing to check is if there is any null object. Because if there is any null object the execution will not be performed, but who does this validation is the database (consumes an id). In this case as there is more than one, one of the executions may have been successful.

I hope I’ve helped.

Browser other questions tagged

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