今天將介紹一款開源組件FluentMigrator,其提供了jQuery式鏈式編程方式,和3.0后的表達式語法使其語義清晰。主要提供我們隊數據庫結構的維護,版本控制回滾和新增。適用於 敏捷和TDD實踐中我們的需求功能的遞增,數據結構增加,可持續化集成,應用場景感覺如其名Fluent(流暢)。
一:我們先利用NuGet安裝FluentMigrator:
1:在vs在打開Package Manager Console:
2:安裝FluentMigrator:
3:如果你希望控制台提交,可以安裝其tools:
二:下面我面做一個簡單的實例訂單Order(這里僅列出其部分字段,不會考慮實際業務):
DO:
using System;
namespace FluentMigratorTest
{
public class Orders
{
public int ID { get; set; }
public string CustomerID { get; set; }
public decimal DisCount { get; set; }
public DateTime OrderDate { get; set; }
}
}
namespace FluentMigratorTest
{
public class Orders
{
public int ID { get; set; }
public string CustomerID { get; set; }
public decimal DisCount { get; set; }
public DateTime OrderDate { get; set; }
}
}
表結構塊:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentMigrator;
namespace FluentMigratorTest
{
[Migration( 0)]
public class OrderMigration:Migration
{
public override void Up()
{
Create.Table( " Orders_test ")
.WithColumn( " ID ").AsInt32().Identity().PrimaryKey( " id_pk ").Indexed( " Orders_Pk_ID ")
.WithColumn( " CustomerID ").AsString().ForeignKey( " Customers ", " CustomerID ").NotNullable()
.WithColumn( " DisCount ").AsDecimal().WithDefaultValue( 0)
.WithColumn( " OrderDate ").AsDateTime().WithDefault(SystemMethods.CurrentDateTime);
}
public override void Down()
{
Delete.Table( " Orders_test ");
}
}
}
using System.Linq;
using System.Text;
using FluentMigrator;
namespace FluentMigratorTest
{
[Migration( 0)]
public class OrderMigration:Migration
{
public override void Up()
{
Create.Table( " Orders_test ")
.WithColumn( " ID ").AsInt32().Identity().PrimaryKey( " id_pk ").Indexed( " Orders_Pk_ID ")
.WithColumn( " CustomerID ").AsString().ForeignKey( " Customers ", " CustomerID ").NotNullable()
.WithColumn( " DisCount ").AsDecimal().WithDefaultValue( 0)
.WithColumn( " OrderDate ").AsDateTime().WithDefault(SystemMethods.CurrentDateTime);
}
public override void Down()
{
Delete.Table( " Orders_test ");
}
}
}
其提供了Up版本遞增和Down回滾。語法是不是很流暢?其不僅這些功能還提供了:
對表結構的新增,修改,刪除,執行sql方法。
利用其提供的tools,更新在數據庫
支持數據庫:
並支持Profile,部署開發和測試不通的數據庫。
更多其它信息請參加:https://github.com/schambers/fluentmigrator/wiki