.NET Evolve數(shù)據(jù)庫版本管理工具
1.簡介
提到數(shù)據(jù)庫版本管理,Java
領域開發(fā)首先會想到大名鼎鼎的flyway
。但是它不適用.NET
領域,那么.NET
領域也需要做數(shù)據(jù)庫版本管理,該用什么工具?自行造輪子?.NET
領域的解決方案就是Evolve
,這是一個開源庫。[倉庫地址](GitHub - lecaillon/Evolve: Database migration tool for .NET and .NET Core projects. Inspired by Flyway.)
Evolve 翻譯成中文意為 “進化、演變”
支持數(shù)據(jù)源有PostgreSQL
,SQL Server
,SQLite
,MySQL
,MariaDB
,Cassandra
,CockroachDB
。文檔介紹詳見[官方網(wǎng)站](Getting Started :: Evolve (evolve-db.netlify.app))
2.開始
在NuGet
倉庫搜索Evolve
第一個就是。
下面便是Evolve
的使用方法,非常簡單,只需指定一個數(shù)據(jù)庫連接對象
,數(shù)據(jù)庫腳本(建庫腳本)
腳本通常是DDL sql文件,可用Navicat等可視化工具建庫完成后,導出為sql文件文章來源:http://www.zghlxwxcb.cn/news/detail-662133.html
private static Evolve BuildEvolve(IDbConnection cnx)
{
var evolve = new Evolve((System.Data.Common.DbConnection)cnx, msg => Debug.WriteLine(msg))
{
IsEraseDisabled = true,
// 用于記錄數(shù)據(jù)庫版本記錄的表,指定表名后,會自動創(chuàng)建
MetadataTableName = "db_changelogs"
};
// 指定數(shù)據(jù)庫腳本所在目錄
var dbPaths = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "db_migrations");
if (Directory.Exists(dbPaths) && Directory.GetFiles(dbPaths, "*.sql").Length > 0)
{
evolve.Locations = new[] { dbPaths };
}
else
{
// 未找到數(shù)據(jù)庫腳本的邏輯處理,這里可不做任何處理接返回
evolve.EmbeddedResourceAssemblies = new Assembly[]
{
typeof(SqlDbClientContext).Assembly
};
}
return evolve;
}
只需在需要初始化數(shù)據(jù)庫的地方調(diào)用上面的方法即可完成數(shù)據(jù)庫版本控制。文章來源地址http://www.zghlxwxcb.cn/news/detail-662133.html
到了這里,關于.NET Evolve 數(shù)據(jù)庫版本管理工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!