VAAA
VAAA

Reputation: 15039

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?

Upvotes: 5

Views: 1631

Answers (1)

Pavitar
Pavitar

Reputation: 4374

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

Upvotes: 1

Related Questions