Reputation: 13161
Is it possible to configure an Entity Framework model so that, when creating a database from the model, generated identity columns have an identity increment other than 1? For example, I might have an identity column where I want the sequence of ids to go: 1, 11, 21, 31, ... (counting by 10 instead of 1).
I am not too concerned about the identity seed since I can easily re-seed a table with a sql statement after EF generates the db schema. However, it appears that if I'd like to change an identity increment, then I must re-create the table (at least in Microsoft Sql Server). This could be somewhat complex to do automatically because of foreign key relationships.
So is there any way to configure identity columns in EF? If not, I'd also be OK with inspecting and modifying the schema creation scripts before they're executed against the db if that's possible.
Update with some additional details:
I have already overridden the Seed
method exposed by DropCreateDatabaseIfModelChanges
(an included implementer of IDatabaseInitializer
). Here I run a few custom initialization routines after the schema is created by EF. I was thinking that I could add another one to modify each identity column specification after creation. However, as noted above, to change the identity increment I have to re-create each table (and its foreign keys). In the absence of an easy way to change an identity column increment after it's created, I'd like to configure the identity columns before they're created so EF creates them with an increment other than 1, or alternatively modify the table creation scripts produced by EF before they're executed.
Upvotes: 5
Views: 1405
Reputation: 364349
You cannot change the seed - EF doesn't allow that. Also changing the seed on the existing table means dropping the column and creating it again. So the answer to this part is no.
Creating scripts "is possible" but you will lost a lot of features EF is doing for you. You will probably lose:
Check this article about creating initializer for existing database which retrievs
Upvotes: 3