Entity Framework 5 code first newsequentialid () as PK

How can I use newsequentialid() as the default value in a PK column?

I have this annotation:

 [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 

But this will generate random guid. I would like to have consecutive guid.

I can manually set the database to newsequentialid() as the default, but is there a better option? Or did the EF team forget about this option?

+7
source share
1 answer

If you use migrations, you can simply change the code-based wrapping to use newsequentialid() as DefaultValueSql .

 public override void Up() { CreateTable( "SomeTable", c => new { Id = c.Guid(nullable: false, defaultValueSql: "newsequentialid()"), }) .PrimaryKey(t => t.Id) ); } 

If you are not using migrations check the question .

+14
source

All Articles