3

How can I create an entity using EF code first that generates a table with a specific identity seed. In this case, I need the seed to be 100.

I have tried injecting the following code into the initial migration:

Sql("DBCC CHECKIDENT ('MyTable', RESEED, 100)");

The update-database command alerts me that "DBCC command 'CHECKIDENT' is not supported in this version of SQL Server", suggesting to me that CHECKIDENT is not supported for SQL Azure.

Does anyone know how to either set the seed of a new table with a data annotation, the fluent migrations api or directly through SQL?

EDIT ***

This is a restatement of the question below, but geared towards SQL Azure: EF Code First - how to set identity seed?

Independent of the target database technology, I would expected to see a far simpler solution, such as the following:

//EF Code First data annotation
//KeyAttribute would have to be unsealed
public class PrimaryKeyAttribute : KeyAttribute
{ 
     public int Seed { get; private set; }
     public int Increment { get; private set; }
     public PrimaryKeyAttribute(int seed = 1, int increment = 1)
     {
         this.Seed = seed;
         this.Increment = increment;
     }
}

//Entity Primary Key Property
[PrimaryKey(100, 1)]
public int ID { get; set; }

I've searched, but I can find no such thing.

I'd even accept some sort of modification of the DatabaseGeneratedAttribute:

//Entity Primary Key Property
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity, 100, 1)]
public int ID { get; set; }

Obviously (for anyone who has used the DatabaseGeneratedAttribute) this is not possible.

If anyone knows something on this matter that I am missing, please let me know! Thanks.

Community
  • 1
  • 1
Doug
  • 2,441
  • 2
  • 19
  • 22
  • Duplicate of http://stackoverflow.com/questions/12415434/how-to-reset-identity-seed-in-sql-azure – ErikEJ Jun 06 '14 at 19:26
  • They are related questions. But seeing as how I am using EF code first, the solution could look like C# attribute or a C# API call. The solution to your referenced question would never look like that. – Doug Jun 07 '14 at 20:30
  • Well, eventually EF would also have to execute some SQL to perform its actions, C# cannot change anything in a database on its own (except SQLCLR) – ErikEJ Jun 08 '14 at 06:56
  • True, but that doesn't make the EF solution obvious, nor the raw SQL injection the appropriate solution for EF. This question should, and will eventually have a different answer. If raw SQL is all there is right now, then so be it. But the fact remains that these are different questions. – Doug Jun 09 '14 at 08:37
  • Well the bottom line is that reseeding a table on Sql Azure is not possible at the moment – ErikEJ Jun 09 '14 at 13:40
  • Thanks. It is possible to add a seed to an identity when creating the table though? That is actually my current use case. Reseeding is only necessary if I can't simply set it in the first place. Do you know if that is possible? – Doug Jun 09 '14 at 15:56
  • Looks like you can set it via alter command, if you make a custom initializer... http://msdn.microsoft.com/en-us/library/azure/ee336286.aspx – ErikEJ Jun 09 '14 at 16:24

0 Answers0