How to change Table Definition using Fluent Migrator

user1589906 picture user1589906 · Jun 6, 2013 · Viewed 10k times · Source

I'm trying to study Fluent Migrator myself and I'm stuck with a problem I need to change the structure of the table which is created by Fluent migrator means I created a table named user using the following code

 [Migration(201306041130)]
    public class BussinessMigrator : Migration
    {
        public override void Up()
        {
            Create.Table("user")
                .WithColumn("id").AsInt32().PrimaryKey().NotNullable().Identity()
                .WithColumn("name").AsString().NotNullable()
                .WithColumn("email_id").AsString().NotNullable()
                .WithColumn("phone").AsInt32().NotNullable()
                .WithColumn("address").AsString().NotNullable()
                .WithColumn("company_id").AsInt32().NotNullable();
        }

    }

Now I need to add 3 more coloums named avatar_name,avatar_type,avatar_data after the column name how can I do that please answer with an example

Thanks in advance

Answer

Daniel Lee picture Daniel Lee · Jun 6, 2013

There are two ways to do this. Either using the Alter.Table expression or the Create.Column expression.

Here is an example from the wiki for Alter.Table:

Alter.Table("Bar")
    .AddColumn("SomeDate")
    .AsDateTime()
    .Nullable();

And here is an example of Create.Column:

Create.Column("avatar_name")
    .OnTable("user")
    .AsString()
    .Nullable();

Generally with FluentMigrator you can lean on Intellisense to help you out. For example, if you start by typing in Create and dot then Intellisense should show you Column, ForeignKey, Index, PrimaryKey, Schema, Sequence, Table and UniqueConstraint. Most of the time this process should be very discoverable and obvious.

There are seven roots that you can start with: Create, Alter, Delete, Execute, Rename, Insert and Update.