Fluent Migrator 在错误的数据库中创建了 table 版本信息

Fluent Migrator created table Version Info in wrong DB

当我连接到 SQL 服务器时,Fluent Migrator 在 master 数据库中创建了一个 Version Info table,但我自己使用 SQL 脚本,然后应用程序会因为找不到 version info table.

而挂掉

我需要做什么才能让我的脚本正确 运行?

C#代码:

static void Main(string[] args)
{
    string connString = @"Server=localhost\SQLEXPRESS;Trusted_Connection=True;";
    //Migration
}

private static IServiceProvider CreateServices(string connString)
{
    return new ServiceCollection()
        // Add common FluentMigrator services
        .AddFluentMigratorCore()
        .ConfigureRunner(rb => rb
            .AddSqlServer()
            // Set the connection string
            .WithGlobalConnectionString(connString)
            // Define the assembly containing the migrations
            .ScanIn(Assembly.GetExecutingAssembly()).For.Migrations())
        // Enable logging to console in the FluentMigrator way
        .AddLogging(lb => lb.AddFluentMigratorConsole())
        // Build the service provider
        .BuildServiceProvider(false);
}

/// <summary>
/// Update the database
/// </summary>
private static void UpdateDatabase(IServiceProvider serviceProvider)
{
    // Instantiate the runner
    runner.MigrateUp(1610594913);
    runner.MigrateUp(1610594914);
}

[Migration(1610594913, TransactionBehavior.None)]
public class Migration_1610594911 : Migration
{
    public override void Down()
    {
        Execute.Sql("DROP DATABASE siteDB");
    }

    public override void Up()
    {
        Execute.Sql(@"CREATE DATABASE siteDB;");
    }
}

第二个SQL:

[Migration(1610594914)]
public class CreateTablesMigation : Migration
{
    public override void Down()
    {
    }

    public override void Up()
    {
        Execute.Sql(@"
USE siteDB;
CREATE TABLE[AspNetRoles]
...");
    }
}

您在 FluentMigrator 之外创建数据库,然后通过提供连接字符串参数 Initial Catalog=xxxDatabase=xxx(它们的意思相同)指定要使用的数据库

在 运行 迁移

之前,使用 SqlCommand 创建数据库的方式与 运行 任何正常 SQL 相同