c# - 如何使用 EF 迁移将 int ID 列更改为 Guid?

标签 c# asp.net entity-framework entity-framework-migrations

我正在使用 EF 代码优先方法并想将 Id 字段更改为 guid 但似乎无法通过以下错误。

这是我的第一次迁移:

public partial class CreateDownloadToken : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.DownloadTokens",
            c => new
            {
                Id = c.Int(nullable: false, identity: true),
                FileId = c.Int(),
                UserId = c.String(nullable: false, maxLength: 128),
                ValidUntil = c.DateTime(nullable: false),
            })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Files", t => t.FileId)
            .ForeignKey("dbo.Users", t => t.UserId, cascadeDelete: true)
            .Index(t => t.FileId)
            .Index(t => t.UserId);

    }

    public override void Down()
    {
        DropForeignKey("dbo.DownloadTokens", "UserId", "dbo.Users");
        DropForeignKey("dbo.DownloadTokens", "FileId", "dbo.Files");
        DropIndex("dbo.DownloadTokens", new[] { "UserId" });
        DropIndex("dbo.DownloadTokens", new[] { "FileId" });
        DropTable("dbo.DownloadTokens");
    }
}

后来我意识到我需要将我的 Id 列设置为 GUID,所以我更改了我的模型文件:

public class DownloadToken
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public Guid Id { get; set; }

    public int? FileId { get; set; }

    [ForeignKey("FileId")]
    public virtual File File { get; set; }

    [Required]
    public string UserId { get; set; }

    [ForeignKey("UserId")]
    public virtual User User { get; set; }

    [Required]
    public DateTime ValidUntil { get; set; }
}

当运行 Add-Migration ChangeDownloadTokenIdToGuid 时,它会生成此文件:

public partial class ChangeDownloadTokenIdToGuid : DbMigration
{
    public override void Up()
    {
        DropPrimaryKey("dbo.DownloadTokens");
        AlterColumn("dbo.DownloadTokens", "Id", c => c.Guid(nullable: false));
        AddPrimaryKey("dbo.DownloadTokens", "Id");
    }

    public override void Down()
    {
        DropPrimaryKey("dbo.DownloadTokens");
        AlterColumn("dbo.DownloadTokens", "Id", c => c.Int(nullable: false, identity: true));
        AddPrimaryKey("dbo.DownloadTokens", "Id");
    }
}

使用 Update-Database 运行此文件会导致此错误:

Identity column 'Id' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, and constrained to be nonnullable.

知道为什么会发生这种情况吗?

最佳答案

这是因为无法将Id列的先前int类型转换为Guid类型(正是尝试执行AlterColumn 方法)。此外,错误消息提示您,Id 列的新类型可以是集合中的一种类型:int、bigint、smallint、tinyint,或者小数或小数位数为 0,对他们来说,可以从int类型进行转换。

解决方案 - 只需删除 Id 列,然后使用新的 Guid 类型重新创建它,以这种方式更改迁移:

public partial class ChangeDownloadTokenIdToGuid : DbMigration
{
    public override void Up()
    {
        DropPrimaryKey("dbo.DownloadTokens");

        DropColumn("dbo.DownloadTokens", "Id");
        AddColumn("dbo.DownloadTokens", "Id", c => c.Guid(nullable: false, identity: true));

        AddPrimaryKey("dbo.DownloadTokens", "Id");
    }

    public override void Down()
    {
        DropPrimaryKey("dbo.DownloadTokens");

        DropColumn("dbo.DownloadTokens", "Id");
        AddColumn("dbo.DownloadTokens", "Id", c => c.Int(nullable: false, identity: true));

        AddPrimaryKey("dbo.DownloadTokens", "Id");
    }
}

P.S. Why you use DatabaseGeneratedOption.Computed attribute, not DatabaseGeneratedOption.Identity?

关于c# - 如何使用 EF 迁移将 int ID 列更改为 Guid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36022901/

相关文章:

c# - Presenter 是否在 MVP 模式中执行 GUI 逻辑?

c# - 如何使用 C# 在 IIS 中获取网站的 "Browse"URL?

asp.net - 在 ASP.NET 中转换 HTML 特殊字符

asp.net - 如何使用 Visual Studio 2013/2015 启动和运行 ASP.NET MVC 音乐商店?

c# - 如何将多态引入实体数据模型

c# - 使用children更新数据库条目

c# - 拜耳有序抖动

c# - IComparable.CompareTo 考虑继承的正确实现

c# - Entity Framework Core 在 QueryType(数据库 View )上使用 Include

c# - 性能:System.Diagnostics.Process 与 System.Management.Automation.PowerShell