DbUp is a nice open source project that allows you to deploy Sql-scripts via a .Net executable. Also it tracks which scripts have already been run on your database so you don’t have to worry about double-updates to your database.
Just create console App (.Net Framework) and install DbUp package using NuGet package manager.
Add the below code in your Program–> Main() method
You may receive following errors while building the project-
#1 – The name ‘ConfigurationManager’ does not exist in the current context
#2 – Since ‘{method}’ returns void, a return keyword must not be followed by an object expression
var connectionString = args.FirstOrDefault() ?? ConfigurationManager.ConnectionStrings["dbup"].ConnectionString; var upgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()) .LogToConsole() .Build(); var result = upgrader.PerformUpgrade(); if (!result.Successful) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(result.Error); Console.ResetColor(); #if DEBUG Console.ReadLine(); #endif return -1; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Success!"); Console.ResetColor(); return 0; }
Solution for these is as below-
#1 – you have to include System.Configuration namespace and also add the reference to the assembly System.Configuration.dll.
#2 – The Main method return type should be changed to int instead of void.
The final code should look like-
using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using DbUp; using System.Configuration; namespace CalcDbUp { class Program { static int Main(string[] args) { var connectionString = args.FirstOrDefault() ?? ConfigurationManager.ConnectionStrings["dbup"].ConnectionString; var upgrader = DeployChanges.To .SqlDatabase(connectionString) .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly()) .LogToConsole() .Build(); var result = upgrader.PerformUpgrade(); if (!result.Successful) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(result.Error); Console.ResetColor(); #if DEBUG Console.ReadLine(); #endif return -1; } Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Success!"); Console.ResetColor(); return 0; } } }