-
Notifications
You must be signed in to change notification settings - Fork 0
Fluent Interface
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:
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();
Create.ForeignKey() // You can give the FK a name or just let Fluent Migrator default to one
.FromTable("Users").ForeignColumn("CompanyId")
.ToTable("Company").PrimaryColumn("Id");
Example extensions can be found in the example MigrationExtensions.cs.
Otherwise, you can replace WithIdColumn with
.WithColumn("Id").AsInt32().NotNullable().PrimaryKey().Identity();
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();
Allows you to delete a table, column, foreign key and schema.
Delete.Table("Users");
Delete multiple columns from a table using the syntax in this expression:
Delete.Column("AllowSubscription").Column("SubscriptionDate").FromTable("Users");
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.Script("myscript.sql");
Execute.EmbeddedScript("UpdateLegacySP.sql");
Execute.Sql("DELETE TABLE Users");
Allows you to rename a column or table.
Rename.Table("Users").To("UsersNew");
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("User").IsNull("Username"); //Delete all rows where Username is null
Update.Table("Users").Set(new { Name = "John" }).Where(new { Name = "Johnanna" });
A common task is to add a non-nullable column without a default value. There are three steps:
1. Add new nullable column.
Alter.Table("Bar")
.AddColumn("SomeDate")
.AsDateTime()
.Nullable();
2. Update all rows to an initial value using the AllRows attribute (not in the Nuget version yet).
Update.Table("Bar")
.Set(new { SomeDate = DateTime.Today })
.AllRows();
3. Change the column to be non-nullable.
Alter.Table("Bar")
.AlterColumn("SomeDate")
.AsDateTime()
.NotNullable();
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")
.Create.Table("Users")
.WithIdColumn()
.WithColumn("Name").AsString().NotNullable();
IfDatabase("Sqlite")
.Create.Table("Users")
.WithColumn("Id").AsInt16().PrimaryKey()
.WithColumn("Name").AsString().NotNullable();
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())
{
this.Create.Column("FirstName").OnTable("Users").AsAnsiString(128).Nullable();
}
Next up, Profiles are migrations that if specified, will always run regardless of what other migrations run.
- Getting FluentMigrator
- How to create a Migration
- Fluent Interface
- Migration Runners
- Use inbuilt database functions when setting the default value
- Sql Server Specific Extensions
- Raw Sql Helper for inserting data
- Auto Reversing Migrations
- Resharper File Template
- Transaction Modes for the Migration Runner
- ApplicationContext: Passing parameters to Migrations
- Dealing with Multiple Database Types
- Filter migrations run based on Tags
- Enforce migration version numbering rules
- Create custom metadata for the VersionInfo table