Entity Framework uses newsequentialid() for Guid Key

c# ef-code-first entity-framework entity-framework-6

Question

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
2
11/25/2017 7:39:48 AM

Accepted Answer

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);
1
11/25/2017 11:27:31 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow