Skip to content
sklose edited this page Jan 3, 2011 · 3 revisions

Project Description NMigrations makes Ruby on Rails' concept of migrations available for .NET applications. The migrations concept enables developers to keep track of schema changes of a database easily. Currenty supported features are:

  • Add / Alter / Drop Tables
  • Add / Alter / Drop Columns
  • Add / Drop Indices
  • Add / Drop Foreign Keys
  • Add / Drop Primary Keys
  • Add / Drop Unique Constraints
  • Insert/Update rows
  • Execute custom SQL
  • Full access to the underlying ADO.NET DbConnection
using NMigrations;
using NMigrations.Sql.SqlServer;

[Migration(1)]
public class InitialMigration : IMigration
{
	public void Up(Database db)
	{
		var t = db.AddTable("MaritalStates");
		t.AddColumn("MaritalStateID", SqlTypes.Int).PrimaryKey().AutoIncrement();
		t.AddColumn("Name", SqlTypes.NVarChar, 32).NotNull();
		
		t.EnableIdentityInsert();
		t.Insert(new { MaritalStateID = 1, Name = "Single" });
		t.Insert(new { MaritalStateID = 2, Name = "Widowed" });
		t.Insert(new { MaritalStateID = 3, Name = "Married" });
		t.DisableIdentityInsert();
	
		t = db.AddTable("Employees");
		t.AddColumn("EmployeeID", SqlTypes.Int).PrimaryKey().AutoIncrement();
		t.AddColumn("Name", SqlTypes.NVarCharMax).NotNull();
		t.AddColumn("Birthday", SqlTypes.Date).NotNull();
		t.AddColumn("MaritalStateID", SqlTypes.Int).NotNull().References("MaritalStates", "MaritalStateID");
	}
	
	public void Down(Database db)
	{
		db.DropTable("Employees");
		db.DropTable("MaritalStates");
	}
}
Clone this wiki locally