When I connected to SQL Server, Fluent Migrator creates a Version Info
table in the master
database, but I create the database myself with a SQL script, then the app drops because it can't find the version info
table.
What do I need to do for my script to run correctly?
C# code:
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;");
}
}
Second SQL:
[Migration(1610594914)]
public class CreateTablesMigation : Migration
{
public override void Down()
{
}
public override void Up()
{
Execute.Sql(@"
USE siteDB;
CREATE TABLE[AspNetRoles]
...");
}
}
You create the database outside of FluentMigrator, then specify the database to use, by providing a connection string parameter Initial Catalog=xxx
or Database=xxx
(they mean the same thing)
Creating the db is done by the same way you'd run any normal SQL, using a SqlCommand, before you run your migrations