5

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.

Michael Petito
  • 12,891
  • 4
  • 40
  • 54
  • Duplicate of http://stackoverflow.com/questions/5275306/does-entity-framework-4-code-first-have-support-for-identity-generators-like-nhi – Tomas Voracek Nov 17 '11 at 00:49
  • @TomasVoracek: I'm not looking to create a new identity generation strategy (i.e. HiLo etc) as asked in your linked question; I'm looking to configure an existing strategy. – Michael Petito Nov 17 '11 at 00:54
  • "Is there any way to configure identity columns in EF?" as you can see in linked question, there is no default way how to do this in code-first, you must write some code. – Tomas Voracek Nov 17 '11 at 00:56

1 Answers1

3

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:

  • Database creation - you will only get script to create tables
  • Database consistency check (EdmMetadata support)

Check this article about creating initializer for existing database which retrievs

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • 1
    "You cannot change the seed - EF doesn't allow that." -- Can you cite a source here? It seems to me like EF is completely unaware of how identity columns are implemented in the database, including the specification of seed or increment values. All EF seems to care about is that the column is database generated, so that it may retrieve the value after insertion, and that it is an identity. It just happens to be that the default specification in Sql Server is IDENTITY (1, 1). I'm going to update my question with some additional details as to what I'm after... – Michael Petito Nov 17 '11 at 19:22
  • I ultimately ended up scripting the entire database schema generated by EF using SQL SMO, modifying the identity specification in the create table statements, and dropping / re-creating the database. Would be nice to have support for this in the future, but for now this seemed like the only option. – Michael Petito Dec 19 '11 at 23:12
  • @Michael: +5! I can't make EF 4.1 CF respect my `identity(1,1)`. `DbSet.Create().ID` returns 0. – abatishchev Dec 21 '11 at 17:07
  • @abatishchev: It has nothing to do with identity. `Create` will create proxied instance of entity in memory of your application and until you add the entity back to the context and use `SaveChanges` there will be no database level seed involved. – Ladislav Mrnka Dec 21 '11 at 20:05
  • @Ladislav: Sure, you're right, thanks. My mistake, I was getting FK constraint violation, I thought that's because of identity, but actually by another reason. – abatishchev Dec 22 '11 at 07:18