天天看點

.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();
        }
    }
}