Entity Framework uses newsequentialid() for Guid Key

Viewed 3595

Using below code,

public class UserProfile
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    // more properties here
}

I thought EF is using newguid(), but when I check database, I see the default value is newsequentialid()

Is this the default behavior of EF? Or it is because I'm using SQL Server 2017?

1 Answers

You're right, by the default newsequentialid() is used as a default value for GUID properties. newsequentialid() is recommended over newid() mostly because of performance considerations.

If you want to have newid() as default value you could achieve this by enabling migrations and specifying defaultValueSql in CreateTable():

CreateTable(
    "dbo.UserProfiles",
    c => new
        {
            Id = c.Guid(nullable: false, identity: true, defaultValueSql: "newid()"),
            Name = c.String(),
        })
    .PrimaryKey(t => t.Id);
Related