天天看點

開源DataBase元件:FluentMigrator

一:我們先利用NuGet安裝FluentMigrator:

1:在vs在打開Package Manager Console:

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/201205/201205271905348864.png"></a>

2:安裝FluentMigrator:

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/201205/201205271905365253.png"></a>

3:如果你希望控制台送出,可以安裝其tools:

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/201205/201205271905379614.png"></a>

二:下面我面做一個簡單的執行個體訂單Order(這裡僅列出其部分字段,不會考慮實際業務):

DO:

<a></a>

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; } 

    } 

}

 表結構塊:

using System.Collections.Generic; 

using System.Linq; 

using System.Text; 

using FluentMigrator; 

    [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復原。文法是不是很流暢?其不僅這些功能還提供了:

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/201205/201205271905387879.png"></a>

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/201205/20120527190539222.png"></a>

對表結構的新增,修改,删除,執行sql方法。

利用其提供的tools,更新在資料庫

<a href="http://images.cnblogs.com/cnblogs_com/whitewolf/201205/201205271905405663.png"></a>

支援資料庫:

sqlserver2000

sqlserver2005

sqlserver2008

sqlserverce

sqlserver

mysql

postgres

oracle

sqlite

jet

并支援Profile,部署開發和測試不通的資料庫。

本文轉自破狼部落格園部落格,原文連結:http://www.cnblogs.com/whitewolf/archive/2012/05/27/2520422.html,如需轉載請自行聯系原作者