Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Identity Seed value in code-first?

We are using Code First with EF-core and I would like to add a column which has an Identity Seed starting at another value other to 1.

Currently we can set it to auto increment via the EntityTypeBuilder during migrations using:

entityBuilder.Property(e => e.PropertyName).ValueGeneratedOnAdd();

However I cannot find out how to change the identity seed. Does it still need to be updated like it was with other versions of EF? e.g. writing some custom sql and running this during migration?

How to seed identity seed value in entity framework code first for several tables

How do I set Identity seed on an ID column using Entity Framework 4 code first with SQL Compact 4?

In EF-core there does not seem to be code for SqlServerMigrationSqlGenerator > override Generate(AlterTableOperation alterTableOperation)?

like image 319
Tim B James Avatar asked Jan 21 '16 15:01

Tim B James


1 Answers

Update 2020.

After EF Core 3.0 now you have a UseIdentityColumn extension method which can be used for setting the seed and increment values for identity columns.

builder.Property(prop => prop.Id)
            .UseIdentityColumn(10000000, 1);

As per offcial documentation:

UseIdentityColumn Configures the key property to use the SQL Server IDENTITY feature to generate values for new entities, when targeting SQL Server. This method sets the property to be OnAdd.

Link

like image 199
Omkar Shinde Avatar answered Sep 22 '22 02:09

Omkar Shinde