简体   繁体   English

实体框架SaveChanges失败增量标识列

[英]Entity Framework SaveChanges Fail increment identity column

I have a question about the Behavior of Entity Framework when SaveChanges method is executed. 执行SaveChanges方法时,我对实体框架的行为有疑问。

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. 我有一个具有标识列的实体,并且只是意识到如果我调用SaveChanges (对于新插入),并且它失败,那么我的DB中的标识列会增加。

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. 我以前的Identity列值等于7,我调试我的代码和SaveChanges方法失败了5次,因为我错过了插入一个必需的值,当它解决了我发现我的身份列值现在是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? 当Entity Framework无法提交到我的数据库时,有没有办法避免增加标识列值?

Appreciate any help 感谢任何帮助

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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