天天看点

.net core的FluentMigrator数据库版本管理

问题的产生

问题的产生

做开发的早晚总会遇到这个问题。

具体就没有必要赘述。

实践

当前时间2019.4.23。

建立一个基于.net core 2.1的控制台应用程序。

.net core的FluentMigrator数据库版本管理

下图是运行此控制台项目时候的执行内容

命令: dotnet DbMigration.dll (这是运行dotnet core项目的命令的规则,在此不多做解释)

.net core的FluentMigrator数据库版本管理

下面是此控制台程序的Program.cs文件的内容

using System;
using FluentMigrator.Runner;
using Microsoft.Extensions.DependencyInjection;

namespace DbMigration
{
    /*
     * FluentMigrator官网:
     * https://fluentmigrator.github.io/articles/quickstart.html?tabs=runner-in-process
     * https://fluentmigrator.github.io/articles/runners/dotnet-fm.html
     *
     * 代码几乎都来自:
     * https://fluentmigrator.github.io/articles/quickstart.html?tabs=runner-in-process
     */
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var serviceProvider = CreateServices();

            // Put the database update into a scope to ensure
            // that all resources will be disposed.
            using (var scope = serviceProvider.CreateScope())
            {
                UpdateDatabase(scope.ServiceProvider);
            }
        }

        /// <summary>
        /// Configure the dependency injection services
        /// </summary>
        private static IServiceProvider CreateServices()
        {
            return new ServiceCollection()
                // Add common FluentMigrator services
                .AddFluentMigratorCore()
                .ConfigureRunner(rb => rb
                    // Add SQLite support to FluentMigrator
                    .AddMySql5()
                    // Set the connection string
                    .WithGlobalConnectionString("server=1.2.3.4;User Id=user1;password=123;Database=test;Charset=utf8")
                    // Define the assembly containing the migrations
                    .ScanIn(typeof(EventTable).Assembly).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
            var runner = serviceProvider.GetRequiredService<IMigrationRunner>();

            // Execute the migrations
            runner.MigrateUp();
        }
    }
}
      

以及一个数据库表对应的C#的class即EventTable.cs

using System;
using System.Collections.Generic;
using System.Text;
using FluentMigrator;
namespace DbMigration
{
    [Migration(20190423094700)]
    public class EventTable : Migration
    {
        public override void Up()
        {
            Create.Table("Event")
                .WithDescription("event")
                .WithColumn("id").AsInt64().PrimaryKey().Identity()
                .WithColumn("EventId").AsInt64().WithColumnDescription("会议id")
                .WithColumn("EventName").AsString().WithColumnDescription("会议名称")
                .WithColumn("IsDeleted").AsBoolean().WithDefaultValue(false).WithColumnDescription("是否已删除");

        }

        public override void Down()
        {
            throw new NotImplementedException();
        }
    }
}