【问题标题】:Entity Framework uses newsequentialid() for Guid Key实体框架使用 newsequentialid() 作为 Guid Key
【发布时间】:2017-11-25 07:39:48
【问题描述】:

使用下面的代码,

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

    // more properties here
}

我以为 EF 使用的是 newguid(),但是当我检查数据库时,我看到默认值是 newsequentialid()

这是 EF 的默认行为吗?还是因为我使用的是 SQL Server 2017?

【问题讨论】:

    标签: c# entity-framework entity-framework-6 ef-code-first


    【解决方案1】:

    您是对的,默认情况下newsequentialid() 用作 GUID 属性的默认值。推荐使用 newsequentialid() 而不是 newid(),主要是出于性能考虑。

    如果您想将newid() 作为默认值,您可以通过启用迁移并在CreateTable() 中指定defaultValueSql 来实现:

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

    【讨论】:

    • 顺序指导仅在服务器重新启动之前是顺序的!这是正确的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 1970-01-01
    相关资源
    最近更新 更多