0

i'm using EF6 to insert rows in database Mysql 8.0.26 . I've changed a column in table, after that I'm not update model in edmx . So when I try to add a new row and SaveChanges(), the save change go in error using a try and catch, but auto_increment of table increase. Someone know to understand how it can't increase auto_increment if the row can't insert : This is the example of code :

  Using e As New studio_laghezzaEntities4


            Try

                Dim newfasccons As New fasciconsulenza With {
                .Openbyuser = idUser,
                .FKcustomer = cons.CodiceCliente,
                .Opendate = Date.Now,
                .LastUpdateDate = Date.Now,
                .LastUserUpdate = idUser,
                .Praticacons = cons.CodiceFascicolo,
                .Esercente = fasc.Esercente
            }

                e.fasciconsulenza.Add(newfasccons)

                e.SaveChanges()

                Dim newcons As New consulenza With {
                .ProspettoOre = fasc.ProspettoOreLavoro,
                .NoteConsulenza = fasc.AreaDescrittiva,
                .FKuser = idUser,
                .FKfasc = newfasccons.IdDoc,
                .FKInterv = idtipointevento,
                .DataCreazione = Date.Now
            }

                e.consulenza.Add(newcons)

                e.SaveChanges()

                Dim newconsinassoc As New consofsales With {
                .FK_fasc = insertIddoc,
                .FK_oursales = idsaler
            }

                e.consofsales.Add(newconsinassoc)

                e.SaveChanges()

                
            Catch ex As Exception

                esito.inserito = False
                esito.message = ex.Message

            End Try
  End Using
  • Does this answer your question? [Why does MySQL autoincrement increase on failed inserts?](https://stackoverflow.com/questions/2787910/why-does-mysql-autoincrement-increase-on-failed-inserts) – Gert Arnold Jul 19 '22 at 20:53
  • Thank you I've tried to change file .ini, because there are others database in this mysql server, I' m searching a solution from code, with entity framework if it exist – Alberto Zambaiti Jul 20 '22 at 13:32
  • Well, the main point of the duplicate is that it's innate behavior. If you really need sequences w/o holes (usually that's pointless though), don't use `auto_increment`. The accepted answer clearly explains why trying to work around is a snake pit. – Gert Arnold Jul 20 '22 at 13:39

0 Answers0