Database upgrade helper for .NET Core, support MSSQL
$ dotnet add package DbUpgradestatic async Task Main(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetParent(AppContext.BaseDirectory).FullName)
.AddJsonFile("appsettings.json", false)
.Build();
UpgradeEngine.To
// [REQUIRED] set connection to database
.SqlDatabase(configuration.GetConnectionString("Database"))
// [OPTIONAL] create a database if it doesn't exist
.CreateDatabase()
// [REQUIRED] for one of them
.WithScripts(Assembly.GetExecutingAssembly()) // get scripts by assembly
.WithScripts($"{AppDomain.CurrentDomain.BaseDirectory}/Migrations/Once", true) // get scripts by path
// [OPTIONAL]
.LogToConsole()
.LogToFile()
.LogToTable("log")
// [OPTIONAL] defaut values: commandType = CommandType.All and execute = true
.ForCommand(DbUpgrade.CommandType.Create)
.ForCommand(DbUpgrade.CommandType.Alter)
.ForCommand(DbUpgrade.CommandType.Drop, false)
.ForCommand(DbUpgrade.CommandType.Data)
.ForCommand(DbUpgrade.CommandType.Unknown, false)
// [REQUIRED]
.Build();
// var commands = UpgradeEngine.CommandsToRun;
UpgradeEngine.PerformUpgrade();
#if DEBUG
Console.ReadKey();
#endif
}
// For 'WithScriptsFromAssembly' option
<ItemGroup>
<EmbeddedResource Include="Migrations\*.sql" />
</ItemGroup>