Skip to content
daniellee edited this page Jul 7, 2012 · 27 revisions

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.

Create.Table("Users")
    .WithIdColumn() // WithIdColumn is an extension, see below link.
    .WithColumn("Name").AsString().NotNullable();

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

Otherwise, you can replace WithIdColumn with

.WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity()
.

Alter Expression

Allows you to alter existing tables and columns.

Alter.Table("Bar")
    .AddColumn("SomeDate")
    .AsDateTime()
    .Nullable();
Alter.Table("Bar")
    .AlterColumn("SomeDate")
    .AsDateTime()
    .NotNullable();
Alter.Column("SomeDate")
    .OnTable("Bar")
    .AsDateTime()
    .NotNullable();

Delete Expression

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

Delete.Table("Users");

Execute Expression

Allows you to execute a block of sql, or a script by name (ie. myscript.sql)

Execute.Script("myscript.sql");

Execute.Sql("DELETE TABLE Users");

Rename Expression

Allows you to rename a column or table.

Rename.Table("Users").To("UsersNew");

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
Update.Table("Users").Set(new { Name = "John" }).Where(new { Name = "Johnanna" });

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.

IfDatabase("SqlServer", "Postgres")
    .Create.Table("Users")
    .WithIdColumn()
    .WithColumn("Name").AsString().NotNullable();

IfDatabase("Sqlite")
    .Create.Table("Users")
    .WithColumn("Id").AsInt16().PrimaryKey()
    .WithColumn("Name").AsString().NotNullable();

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

Clone this wiki locally