Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework uses newsequentialid() for Guid Key

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?

like image 671
FLICKER Avatar asked Nov 25 '17 07:11

FLICKER


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);
like image 71
CodeFuller Avatar answered Nov 04 '22 12:11

CodeFuller