我尝试运行实体框架迁移:
public override void Up()
{
AlterColumn("dbo.ThubOutputEmails", "AttachmentName", c => c.String(nullable: true, maxLength: 256));
SqlExecute("ALTER TABLE dbo.ThubOutputEmails ADD CONSTRAINT DF_ThubOutputEmail_Created default getdate() for Created");
}
转换为update-database
命令中的以下脚本:
ALTER TABLE [dbo].[ThubOutputEmails] ALTER COLUMN [AttachmentName] [nvarchar](256) NULL
EXECUTE('
ALTER TABLE dbo.ThubOutputEmails ADD CONSTRAINT DF_ThubOutputEmail_Created default getdate() for Created
')
它失败并显示消息:
Column already has a DEFAULT bound to it
问题是列上没有数据库约束,但迁移报告已存在这样的约束。当我复制并粘贴SQL并在SQL Server Management Studio中运行它时,它运行正常并创建约束。