我正在使用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.
为什么会发生这种情况的任何想法?
答案 0 :(得分:21)
这是因为无法将之前int
类型的Id
列转换为Guid
类型(正好尝试执行AlterColumn
方法)。此外,错误消息建议您,新类型的Id
列可以是set中的类型之一: 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。为什么使用
DatabaseGeneratedOption.Computed
属性,而不是DatabaseGeneratedOption.Identity
?
答案 1 :(得分:10)
即使Slava Utesinov的作品,它只适用于空表或没有其他表指的是您正在转换的表。因此,这个答案将帮助那些以更复杂的数据库设置结束此页面的人。
以下是您可以在迁移类中使用的实用程序函数,该函数应该从Up / Down函数调用。该函数还处理表引用您尝试从Int转换为Guid的表。这个辅助函数假定您要转换的列称为“Id”,但应该是相当通用的。
public void Convert(bool toGuid, string parent, params string[] children)
{
if (toGuid)
{
AddColumn($"dbo.{parent}s", "Id2", c => c.Guid(nullable: false, identity: true, defaultValueSql: "newid()"));
}
else
{
AddColumn($"dbo.{parent}s", "Id2", c => c.Int(nullable: false, identity: true));
}
foreach (var child in children)
{
DropForeignKey($"dbo.{child}s", $"{parent}_Id", $"dbo.{parent}s");
DropIndex($"dbo.{child}s", new[] { $"{parent}_Id" });
RenameColumn($"dbo.{child}s", $"{parent}_Id", $"old_{parent}_Id");
if (toGuid)
{
AddColumn($"dbo.{child}s", $"{parent}_Id", c => c.Guid());
}
else
{
AddColumn($"dbo.{child}s", $"{parent}_Id", c => c.Int());
}
Sql($"update c set {parent}_Id=p.Id2 from {child}s c inner join {parent}s p on p.Id=c.old_{parent}_Id");
DropColumn($"dbo.{child}s", $"old_{parent}_Id");
}
DropPrimaryKey($"dbo.{parent}s");
DropColumn($"dbo.{parent}s", "Id");
RenameColumn($"dbo.{parent}s", "Id2", "Id");
AddPrimaryKey($"dbo.{parent}s", "Id");
foreach (var child in children)
{
CreateIndex($"dbo.{child}s", $"{parent}_Id");
AddForeignKey($"dbo.{child}s", $"{parent}_Id", $"dbo.{parent}s", "Id");
}
}
因此,在您的情况下,您的向上/向下功能将是:
public override void Up()
{
Convert(true,"DownloadToken");
}
public override void Down()
{
Convert(false, "DownloadToken");
}