Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Fluent Interface

ReubenBond edited this page · 29 revisions
Clone this wiki locally

The FM fluent api allows you to create tables, columns, indexes and (nearly) every construct you need to manipulate your database structure.

Behind the scenes, the fluent api populates a semantic model that FM uses to analyze and apply migrations in batch. The fluent api that is available in your Migration class starts with five main root expressions as follows:

Create Expression

Allows you to create a table, column, index, foreign key and schema.

    .WithIdColumn() // WithIdColumn is an extension, see below link.
Create.ForeignKey() // You can give the FK a name or just let Fluent Migrator default to one

Example extensions can be found in the example MigrationExtensions.cs.

Otherwise, you can replace WithIdColumn with


Alter Expression

Allows you to alter existing tables and columns.


Delete Expression

Allows you to delete a table, column, foreign key and schema.


Delete Multiple Columns

Delete multiple columns from a table using the syntax in this expression:


Execute Expression

Allows you to execute a block of sql, or a script by name (ie. myscript.sql) or an embedded sql script. To embed an sql script, add the file to your migration project and change the Build Action property to Embedded Resource.

Execute.Sql("DELETE TABLE Users");

Rename Expression

Allows you to rename a column or table.


Data Expressions

Allows you to insert a row into a table using an anonymous type for the rows contents

Insert.IntoTable("Users").Row(new { FirstName = "John", LastName = "Smith" });
Delete.FromTable("Users").AllRows(); // delete all rows
Delete.FromTable("Users").Row(new { FirstName = "John" }); // delete all rows with FirstName==John
Delete.FromTable("Users").IsNull("Username"); //Delete all rows where Username is null
Update.Table("Users").Set(new { Name = "John" }).Where(new { Name = "Johnanna" });

AllRows Attribute

A common task is to add a non-nullable column without a default value. One way this can be achieved is with the ‘AllRows’ attribute, via these three steps:

1. Add new nullable column.


2. Update all rows to an initial value using the AllRows attribute.
    .Set(new { SomeDate = DateTime.Today })

3. Change the column to be non-nullable.

As of version 1.3.0, this can be done with a single statement using the SetExistingRowsTo method.


IfDatabase Expression

Allows for conditional expressions depending on database type. The current database types supported are:

  • SqlServer (this includes Sql Server 2005 and Sql Server 2008)
  • SqlServer2000
  • SqlServerCe
  • Postgres
  • MySql
  • Oracle
  • Jet
  • Sqlite

Multiple database types (as specified above) can be passed into the IfDatabase Expression (See Dealing-with-Multiple-Database-Types for more details).

IfDatabase("SqlServer", "Postgres")


Schema.Exists Expressions

You can write migrations conditional on the preexisting schema, which comes in handy for getting you out of certain jams. For instance, if you need to make a column but aren’t sure if it already exists:

if (!Schema.Table("Users").Column("FirstName").Exists())

Next up, Profiles are migrations that if specified, will always run regardless of what other migrations run.

Something went wrong with that request. Please try again.