7

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

I have this annotation:

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

But this will generate random guid's I would like to have sequential guid's.

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

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Roger Far
  • 2,178
  • 3
  • 36
  • 67

1 Answers1

15

If you are using migrations you should be able to simply modify code based migration 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 this question.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • i had to add Key and DatabaseGenerated comma seperated to make it work, if they are added seperately, then it not works – Ehsan Sajjad Jun 17 '16 at 02:10