Question

I don't found the solve of my problem in the Internet.

I have a project asp net mvc 3. I have a database, I have a class of similar entities from the database:

public class Product : Entity
{
    [Required(ErrorMessage = "Error!")]
    public virtual string Name { get; set; }

    [Required(ErrorMessage = "Error!")]
    public virtual int Age { get; set; }
}

In my project I want to use migration. In the project I added the library FluentMigrator, created the following class (this is a test class with a test table):

[Migration(1)]
public class Step1 : Migration
{
    public override void Up()
    {
        #region Create Tables
        Create.Table("Products").InSchema("dbo")
            .WithColumn("ProductId").AsInt32().Identity().NotNullable().PrimaryKey()
            .WithColumn("Name").AsString(255).Nullable()
            .WithColumn("Age").AsInt32().Nullable();

        #endregion
        this.Execute.Script("test.sql");
    }

    public override void Down()
    {
        #region Delete Tables
        Delete.Table("Products").InSchema("dbo");
        #endregion
    }

}

also in my solution I created the file - "test.sql". This file contains the following code:

INSERT INTO [Products] ([Name],[Age])
 VALUES ('test12', 1111)

Tell me please, how can I run the migration in my project, that the code from the file "test.sql" added to the table "Products"? Sorry for my english.

Was it helpful?

Solution

A migration defines the change that you want to make to the database. To actually execute it, you have to use one of the runners for FluentMigrator. There are 3 of them. There is a command line tool that comes with the Nuget package and it is run from the command line like this:

.\packages\FluentMigrator.1.1.1.0\tools\migrate -conn "server=.\SQLEXPRESS;uid=test;pwd=test;database=FluentMigratorSample" --provider sqlserver2012 --assembly ".\bin\Debug\FluentMigratorSample.dll"

There are 4 parts to this. .\packages\FluentMigrator.1.1.1.0\tools\migrate is the path in the Nuget package to the migrate.exe. The -conn part is the connection string. The -provider part specifies which type of database it is. The -assemby part is the path to the assembly/dll which contains the migrations that should be executed. See the wiki for more details.

If you are using Nant or Msbuild. There are runners for them as well. They have the same options as the command line tool.

There is one other way to run FluentMigrator and that is directly through code. An example would be something like this class (a friend's project on Github). This class is then called on startup in global.asax.cs like this.

OTHER TIPS

You can use FluentMigrator's command-line tool:

Migrate.exe /connection "Data Source=db\db.sqlite;Version=3;" /db sqlite /target your.migrations.dll
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top