是否可以在application_start中使用流畅的迁移器?

发布于 2024-12-06 23:36:59 字数 893 浏览 1 评论 0原文

我正在使用流畅的迁移器来管理我的数据库迁移,但我想做的是在应用程序启动时运行迁移。我管理过的最接近的是:

public static void MigrateToLatest(string connectionString)
{
    using (var announcer = new TextWriterAnnouncer(Console.Out)
                                {
                                    ShowElapsedTime = true,
                                    ShowSql = true
                                })
    {
        var assembly = typeof(Runner).Assembly.GetName().Name;

        var migrationContext = new RunnerContext(announcer)
        {
            Connection = connectionString,
            Database = "SqlServer2008",
            Target = assembly
        };

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

我确信我已经完成了这个工作,但我有一段时间没有查看它(业余爱好项目),现在当它到达执行 线。遗憾的是,没有这方面的文档,而且我多年来一直在努力解决这个问题。

有没有人设法让这种事情与 FluentMigrator 一起工作?

I'm using fluent migrator to manage my database migrations, but what I'd like to do is have the migrations run at app start. The closest I have managed is this:

public static void MigrateToLatest(string connectionString)
{
    using (var announcer = new TextWriterAnnouncer(Console.Out)
                                {
                                    ShowElapsedTime = true,
                                    ShowSql = true
                                })
    {
        var assembly = typeof(Runner).Assembly.GetName().Name;

        var migrationContext = new RunnerContext(announcer)
        {
            Connection = connectionString,
            Database = "SqlServer2008",
            Target = assembly
        };

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

I'm sure I had this working, but I've not looked at it for sometime (hobby project) and it's now throwing null reference exceptions when it gets to the Execute line. Sadly there are no docs for this and I've been banging my head on it for ages.

Has anyone managed to get this kind of thing working with FluentMigrator?

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

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

发布评论

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

评论(2

甜心 2024-12-13 23:36:59

PM>安装包 FluentMigrator.Tools

手动添加对以下内容的引用:

packages\FluentMigrator.Tools.1.6.1\tools\AnyCPU\40\FluentMigrator.Runner.dll

注意该文件夹名称会因版本号而异,此插图使用当前的 1.6.1 版本。如果您需要 .NET 3.5 运行程序,请使用 \35\ 目录。

public static class Runner
{
    public class MigrationOptions : IMigrationProcessorOptions
    {
        public bool PreviewOnly { get; set; }
        public string ProviderSwitches { get; set; }
        public int Timeout { get; set; }
    }

    public static void MigrateToLatest(string connectionString)
    {
        // var announcer = new NullAnnouncer();
        var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
        var assembly = Assembly.GetExecutingAssembly();

        var migrationContext = new RunnerContext(announcer)
        {
            Namespace = "MyApp.Sql.Migrations"
        };

        var options = new MigrationOptions { PreviewOnly=false, Timeout=60 };
        var factory = 
            new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory();

        using (var processor = factory.Create(connectionString, announcer, options))
        { 
            var runner = new MigrationRunner(assembly, migrationContext, processor);
            runner.MigrateUp(true);
        }
    }
}

请注意,SqlServer2008ProcessorFactory 是可根据您的数据库进行配置的,支持:2000、2005、2008、2012 和 2014。

PM> Install-Package FluentMigrator.Tools

Manually add a reference to:

packages\FluentMigrator.Tools.1.6.1\tools\AnyCPU\40\FluentMigrator.Runner.dll

Note that the folder name will vary on version number, this illustration uses the current 1.6.1 release. If you need the .NET 3.5 runner use the \35\ directory.

public static class Runner
{
    public class MigrationOptions : IMigrationProcessorOptions
    {
        public bool PreviewOnly { get; set; }
        public string ProviderSwitches { get; set; }
        public int Timeout { get; set; }
    }

    public static void MigrateToLatest(string connectionString)
    {
        // var announcer = new NullAnnouncer();
        var announcer = new TextWriterAnnouncer(s => System.Diagnostics.Debug.WriteLine(s));
        var assembly = Assembly.GetExecutingAssembly();

        var migrationContext = new RunnerContext(announcer)
        {
            Namespace = "MyApp.Sql.Migrations"
        };

        var options = new MigrationOptions { PreviewOnly=false, Timeout=60 };
        var factory = 
            new FluentMigrator.Runner.Processors.SqlServer.SqlServer2008ProcessorFactory();

        using (var processor = factory.Create(connectionString, announcer, options))
        { 
            var runner = new MigrationRunner(assembly, migrationContext, processor);
            runner.MigrateUp(true);
        }
    }
}

Note the SqlServer2008ProcessorFactory this is configurable dependent upon your database, there is support for: 2000, 2005, 2008, 2012, and 2014.

夜夜流光相皎洁 2024-12-13 23:36:59

我实际上已经在 application_start 中完成了运行迁移,但是很难从该代码中判断出什么问题......由于它是开源的,我只需获取代码并将其拉入您的解决方案并构建它以找出问题所在执行方法正在抱怨。我发现 Fluent Migrator 的源代码组织得很好。

如果这是一个 Web 应用程序,您可能需要担心的一件事是确保在您迁移时没有人使用数据库。我使用的策略是建立连接,将数据库设置为单用户模式,运行迁移,将数据库设置为多用户模式,然后关闭连接。这还可以处理多台服务器上的负载平衡 Web 应用程序的情况,因此 2 台服务器不会尝试针对同一数据库运行迁移。

I have actually accomplished running migrations in the application_start however it is hard to tell from that code what could be wrong... Since it is open source I would just grab the code and pull it into your solution and build it to find out what the Execute method is complaining about. I found that the source code for Fluent Migrator is organized pretty well.

One thing that you might have to be concerned about if this is a web app is making sure that no one uses the database while you are migrating. I used a strategy of establishing a connection, setting the database to single user mode, running the migrations, setting the database to multi user mode, then closing the connection. This also handles the scenario of a load balanced web application on multiple servers so 2 servers don't try to run migrations against the same database.

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