1
In my application a few times when I will add some data to the database the ID jumps in 1000 drives.
I found some solutions, but they are not good for me because I use EF6 with code-first and my intention is to install the application with Localdb on a machine without "programming components".
What would help me solve this?
Example class:
public class ProdutoDTO
{
public int produtoID { get; set; }
public int codigo { get; set; }
public string descricao { get; set; }
public decimal preco { get; set; }
public DateTime dataCadastro { get; set; }
}
Mapping:
class ProdutoConfig : EntityTypeConfiguration<ProdutoDTO>
{
public ProdutoConfig()
{
HasKey(p => p.produtoID);
Property(p => p.descricao)
.IsRequired()
.HasMaxLength(100);
Property(p => p.preco)
.IsRequired();
}
}
In the project I use: Code first, EF6, Migrations, generic repositories, implementation repositories and mapping with Fluent Api.
As so it is jumping 1000, it would not be a table that has already been created and you are performing several operations without resetting Identity?
– Leandro Angelo
Type, the last product table id is 7, hence I will add another product and instead of id being 8 it becomes 1008
– Márcio Sebastião
always 1000 range? see how’s the table auto increment setting
– Leandro Angelo
Yes, always 1000
– Márcio Sebastião
Display your entity class and your table’s Identity configuration
– Leandro Angelo
Edit on the question, is this your entity model or just a actual DTO? Checked if Seed is not set to 1000?
– Leandro Angelo
I separated into 3 layers and decided to use DTO, so basically this is my template class for table creation
– Márcio Sebastião
I edited the question
– Márcio Sebastião
And the Identity Seed?
– Leandro Angelo
How I see the Seed setting?
– Márcio Sebastião
Run this query in the & #Xa database;
SELECT IDENT_SEED(TABLE_NAME) AS Seed, IDENT_INCR(TABLE_NAME) AS Increment,IDENT_CURRENT(TABLE_NAME) AS Current_Identity
FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'Produto'
– Leandro Angelo
Seed : 1 Increment: 1 Current_identity : 2001
– Márcio Sebastião
Take a look here https://stackoverflow.com/questions/39671899/ids-jumping-up-1000-every-time-i-build-my-app-and-insert-a-new-record
– Leandro Angelo
Possible duplicate of SQL increasing ID by 1000 units
– Renan
@Leandroangelo and Márcio answered an item related to this one: https://answall.com/questions/36881/sql-incrementing-o-id-em-1000-unidades/36890#36890
– Renan
@Renan Boa, killed the riddle.
– Leandro Angelo
Thank you very much to the 2, I will see that now, I think it solves my problem
– Márcio Sebastião
It is an error of SQL Server, usually happens in case of database failures. It has a parameter that should be placed on the service startup, so that the database creates the identity log, avoiding the loss of sequence during failures. check this link, it has the complete dossier : http://www.dfarber.com/computer-consulting-blog/2014/2/13/how-to-solve-identity-problem-in-sql-2012/
– Thiago Santos