c# - Entity Framework 5 代码首先将 newsequentialid() 作为 PK

标签 c# entity-framework newsequentialid

如何使用 newsequentialid() 作为 PK 列中的默认值?

我有这个注释:

[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]

但这会生成随机的 guid,我希望有连续的 guid。

我可以手动将数据库设置为 newsequentialid() 作为默认值,但是没有更好的选择吗?还是 EF 团队忘记了这个选项?

最佳答案

如果您正在使用迁移,您应该能够简单地修改基于代码的迁移以将 newsequentialid() 用作 DefaultValueSql

public override void Up() {
    CreateTable(
        "SomeTable",
         c => new {
            Id = c.Guid(nullable: false, defaultValueSql: "newsequentialid()"),
         })
         .PrimaryKey(t => t.Id)
    );
}

如果您不使用迁移,请检查此 question .

关于c# - Entity Framework 5 代码首先将 newsequentialid() 作为 PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13012099/

相关文章:

javascript - 单击 <li> 调用 c# 方法

c# - 向下舍入到 dateTime 中最近的边界

c# - Xamarin Http 请求仅适用于 iOS 模拟器

c# - IFrame 引用问题 - asp.net c#

visual-studio-2010 - IntelliTrace 和 ADO.NET 参数值

c# - 使用异步/等待方法时使用存储库模式 Asp.NET MVC 5 EF

c# - Entity Framework 添加新实体

sql - newid() 与 Entity Framework 6 Code First

sql-server - UniqueIdentifier 聚集索引上的 NewSequentialId