Search code examples
c#visual-studio-2017fluent-migrator

How to perform migrations in the package manager?


I installed FluentMigration to manage my SQL files.

In the package management I execute the following commands:

PM> dotnet add package FluentMigrator
PM> dotnet add package FluentMigrator.Runner

Migration

 [Migration(201805041513)]
    public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
    {
        public override void Up()
        {
            Create.Table("Pessoa")
                .InSchema("angularCore")
                .WithColumn("Id").AsInt32().Identity()
                .WithColumn("Nome").AsString(80)
                .WithColumn("SobreNome").AsString(50)
                .WithColumn("Email").AsString(50)
                .WithColumn("IdTpoPessoa").AsInt16()
                .WithColumn("IdEndereco").AsInt16();


        }
    }

Out-of-process (for some corporate requirements)

PM> dotnet tool install -g FluentMigrator.DotNet.Cli

Error:

No executable found corresponding to the "dotnet-tool" command

Documentation

Edit

Run in

PM> dotnet tool install -g FluentMigrator.DotNet.Cli
PM> dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"

Generate teste.db

enter image description here

In the old versions you run the migrations directly in the database, I did not understand how to update my database, ie create the Person table through the generated test.db file?


Solution

  • I was able to get this to work without any issues. Here is what I did:

    First I installed .NET Core 2.1-Preview 2. After installing I verified the version:

    dotnet --version
    2.1.300-preview2-008533
    

    I then created the project

    mkdir testfm
    cd testfm
    dotnet new console
    

    Installed the nuget packages

    dotnet add package FluentMigrator
    dotnet add package FluentMigrator.Runner
    dotnet add package FluentMigrator.Runner.SQLite
    dotnet add package Microsoft.Data.Sqlite
    

    Installed the CLI tool

    dotnet tool install -g FluentMigrator.DotNet.Cli
    

    Created a Migration Class called Migration1.cs

         using FluentMigrator;
    
    namespace test
    {
        [Migration(201805041513)]
        public class _201805041513_CriacaoTabelaPessoa : ForwardOnlyMigration
        {
            public override void Up()
            {
                Create.Table("Pessoa")
                    .InSchema("angularCore")
                    .WithColumn("Id").AsInt32().Identity()
                    .WithColumn("Nome").AsString(80)
                    .WithColumn("SobreNome").AsString(50)
                    .WithColumn("Email").AsString(50)
                    .WithColumn("IdTpoPessoa").AsInt16()
                    .WithColumn("IdEndereco").AsInt16();
    
    
            }
        }
    }
    

    Compiled the project

    dotnet build
    

    Ran the migration from the root project directory

    dotnet fm migrate -p sqlite -c "Data Source=test.db" -a ".\bin\Debug\netcoreapp2.1\test.dll"

    I then Received the following messages. Sucess FM Run

    I then confirmed the table was created by viewing the SqlLite DB

    SqlLite DB View

    To run this same migration on a Sql Server 2016 you would run:

    dotnet fm migrate -p SqlServer2016 -c "server=SQLSERVERINSTANCE;uid=testfm;pwd=test;Trusted_Connection=yes;database=FluentMigrator" -a ".\bin\Debug\netcoreapp2.1\test.dll"