我继承了一个使用 FluentMigrator 来管理迁移的项目。最初,当应用程序启动时,该项目正在执行进程中的迁移,但 IT 已经解决了这个问题,我们现在必须为我们所有的数据库更改提供脚本给 DBA。
作为此过渡的一部分,我已将迁移移至一个名为 Migrations 的新项目。当我尝试使用命令行工具执行迁移时,它似乎可以工作,但没有将迁移应用于数据库。数据库字符串是正确的,因为如果 VersionInfo 表不存在,则会创建它。
有许多迁移,但大多数都非常简单。这是第一个的示例:
我正在使用 SQL Server 2012 和 FluentMigrator 1.2.1。
这是 gunr2171 的文本命令行:
.\Packages\FluentMigrator.1.2.1.0\tools\migrate.exe -c "Data Source=.;Integrated Security=True;Initial Catalog=portal_test" -db sqlserver2012 -a .\source\Migrations\bin\Debug\migrations.dll
以及示例迁移:
using System;
using System.Collections.Generic;
using System.Linq;
using FluentMigrator;
namespace Migrations
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1707:IdentifiersShouldNotContainUnderscores")]
[Migration(1)]
public class M001_CreateAccountTable : Migration
{
public override void Up()
{
Create.Table("Accounts")
.WithColumn("Id").AsInt32().NotNullable().Identity().Unique()
.WithColumn("PartnerCode").AsString().Nullable()
.WithColumn("AccountType").AsInt32().NotNullable()
.WithColumn("Code").AsString().NotNullable().Unique().PrimaryKey()
.WithColumn("Name").AsString().NotNullable()
.WithColumn("PrimaryDomainName").AsString().Nullable()
.WithColumn("IsFederated").AsBoolean().NotNullable()
.WithColumn("IsActive").AsBoolean().Nullable().WithDefaultValue(1)
.WithColumn("FederatedEndpoint").AsString().Nullable()
.WithColumn("CreatedBy").AsString().NotNullable()
.WithColumn("CreatedOn").AsDateTime().NotNullable().WithDefaultValue(DateTime.Now)
.WithColumn("ModifiedBy").AsString().NotNullable()
.WithColumn("ModifiedOn").AsDateTime().NotNullable().WithDefaultValue(DateTime.Now);
}
public override void Down()
{
Delete.Table("Accounts");
}
}
}