从代码执行 FluentMigrator 迁移

发布于 2024-08-31 16:15:33 字数 363 浏览 4 评论 0原文

是否有任何教程或示例代码用于从代码内执行 FluentMigrator 迁移?一些“入门...”教程会非常棒。我所能找到的只是 FluentMigrator 源代码中的 FluentMigrator.Tests (单元测试),它不如“入门...”那么有用。

我只想向项目添加几个类并从项目运行迁移,无需外部工具。 Fluent Migrator 中可以吗? 类似的东西吗?

FluentMigrator.Migrate("database path", typeof(Migration024));

我会从 Program.Main() 调用

Are there any tutorials or example code for executing FluentMigrator migrations from within code? Some "Getting Started..." tutorial would be just awesome. All I was able to find was FluentMigrator.Tests (unit tests), inside FluentMigrator source, which are not as helpful as "Getting Started..." would be.

I just want to add few classes to the project and run the migrations from that project, with no external tools. Is it possible in Fluent Migrator? Something like

FluentMigrator.Migrate("database path", typeof(Migration024));

which I would call from Program.Main()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

路弥 2024-09-07 16:15:33

FluentMigrator 的一位原作者刚刚撰写了这篇“入门”博文

One of the original authors of FluentMigrator just wrote this "Getting started" blogpost.

身边 2024-09-07 16:15:33

我从他们的源代码中抄袭了这个...

using (IAnnouncer announcer = new TextWriterAnnouncer(Console.Out))
{
   IRunnerContext migrationContext = new RunnerContext(announcer) 
   { 
      Connection = "Data Source=test.db;Version=3", 
      Database = "sqlite", 
      Target = "migrations" 
   };

   TaskExecutor executor = new TaskExecutor(migrationContext);
   executor.Execute();
}

我在 WiX 的自定义操作类中使用与此类似的代码。目标是要执行的程序集的名称。就您而言,它将是您的迁移项目生成的任何程序集。您可以设置 IRunnerContext 上的其他选项。像命名空间、PreviewOnly 等。不幸的是,它没有文档记录,因此您必须深入研究代码才能弄清楚。我在生成 Migrate.exe 程序集的项目中找到了大部分内容。

I cribbed this from their source code...

using (IAnnouncer announcer = new TextWriterAnnouncer(Console.Out))
{
   IRunnerContext migrationContext = new RunnerContext(announcer) 
   { 
      Connection = "Data Source=test.db;Version=3", 
      Database = "sqlite", 
      Target = "migrations" 
   };

   TaskExecutor executor = new TaskExecutor(migrationContext);
   executor.Execute();
}

I use code similar to this in a custom action class in WiX. Target is the name of the assembly you want to execute. In your case, it would be whatever assembly is produced by your migration project. There are other options on the IRunnerContext you can set. Like Namespace, PreviewOnly, etc. Unfortunately, it isn't documented so you'll have to dig into the code to figure it out. The project that generates the Migrate.exe assembly is where I found most of this.

素食主义者 2024-09-07 16:15:33

下面是一个在 C#(而不是 MSBuild、Nant 或控制台运行程序)中执行此操作的示例,基于 Stackoverflow 上的片段:

static void Main(string[] args)
{
    string connectionString = @"server=.\SQLEXPRESS;database=testdb;uid=sa2;pwd=Passw0rd";
    Announcer announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
    announcer.ShowSql = true;

    Assembly assembly = Assembly.GetExecutingAssembly();
    IRunnerContext migrationContext = new RunnerContext(announcer);

    var options = new ProcessorOptions 
    { 
        PreviewOnly = false,  // set to true to see the SQL
        Timeout = 60 
    };
    var factory = new SqlServer2008ProcessorFactory();
    using (IMigrationProcessor processor = factory.Create(connectionString, announcer, options))
    {
        var runner = new MigrationRunner(assembly, migrationContext, processor);
        runner.MigrateUp(true);

        // Or go back down
        //runner.MigrateDown(0);
    }
}

[Migration(1)]
public class CreateUserTable : Migration
{
    public override void Up()
    {
        Create.Table("person")
            .WithColumn("Id").AsGuid().PrimaryKey()
            .WithColumn("Name").AsString();
    }

    public override void Down()
    {
        Delete.Table("person");
    }
}

在 C# 中使用 TaskExecutor 执行此操作会遇到麻烦,因为该类纯粹是意味着对于控制台应用程序 (migrate.exe)。

Here's an example of doing it in C# (rather than MSBuild, Nant or the console runner), based on scraps on Stackoverflow:

static void Main(string[] args)
{
    string connectionString = @"server=.\SQLEXPRESS;database=testdb;uid=sa2;pwd=Passw0rd";
    Announcer announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
    announcer.ShowSql = true;

    Assembly assembly = Assembly.GetExecutingAssembly();
    IRunnerContext migrationContext = new RunnerContext(announcer);

    var options = new ProcessorOptions 
    { 
        PreviewOnly = false,  // set to true to see the SQL
        Timeout = 60 
    };
    var factory = new SqlServer2008ProcessorFactory();
    using (IMigrationProcessor processor = factory.Create(connectionString, announcer, options))
    {
        var runner = new MigrationRunner(assembly, migrationContext, processor);
        runner.MigrateUp(true);

        // Or go back down
        //runner.MigrateDown(0);
    }
}

[Migration(1)]
public class CreateUserTable : Migration
{
    public override void Up()
    {
        Create.Table("person")
            .WithColumn("Id").AsGuid().PrimaryKey()
            .WithColumn("Name").AsString();
    }

    public override void Down()
    {
        Delete.Table("person");
    }
}

You'll have troubles doing it in C# with TaskExecutor as that class is meant purely for the console app (migrate.exe).

很酷又爱笑 2024-09-07 16:15:33

由于 Fluent Migrator 是 Migrator .NET 的一个分支,您可能会找到以下内容的入门: Migrator .net 有帮助

Since fluent migrator is a fork of Migrator .NET you might find the getting started for Migrator .net helpful

清风不识月 2024-09-07 16:15:33

本教程对我了解如何构建和如果您使用的是 Visual Studio,请将 FluentMigrator 与 MSBuild 结合使用。

还附带了备份和恢复数据库的示例。

This tutorial was useful for me to figure out how to build and use FluentMigrator with MSBuild, if you are using visual studios.

Also comes with the an example of backing up and restoring a database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文