简体   繁体   中英

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.

在此输入图像描述

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

这是Sql的行为,而不是实体框架。你应该看看Dbreseed命令并为你的表执行它,如果你不想让身份被浪费。检查这个问题RESEED数据库上的标识列如果我是你我不会打扰。只需使用Bigint数据类型作为标识列

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM