Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework SaveChanges Fail increment identity column

I have a question about the Behavior of Entity Framework when SaveChanges method is executed.

I have an entity that has an Identity Column, and just realize that when if I call SaveChanges (for a new insert), and it fails then my Identity Column in my DB is increased.

I used to have the Identity column value equals 7, and I was debugging my code and SaveChanges method failed 5 times, because I missed to insert a required value, and when it worked out I noticed that my identity column value was now 13.

enter image description here

Is this a normal behavior? Is there a way to avoid increasing identity column value when Entity Framework fail to do a commit into my DB?

Appreciate any help

like image 251
VAAA Avatar asked Nov 11 '22 01:11

VAAA


1 Answers

This is Sql's behaviour and not Entity Framework.You should have a look at Dbreseed command and execute it for your table if you don't want to Identity to be wasted.check this So question RESEED identity columns on the database If I were you I wouldn't have bothered.Instead just use Bigint datatype for the identity column

like image 91
Pavitar Avatar answered Nov 14 '22 23:11

Pavitar