Migrate via ASP.NET Core App

Run your database migration when your ASP.NET Core host service starts up. This ensures that database is always at latest compatible state before operating the service.

Pre-requisites

Prepare your database

Deploy an SQL Server on Linux container or use your preferred instance.

docker run -dit -e "ACCEPT_EULA=Y" -e "MSSQL_SA_PASSWORD=P@ssw0rd!" -p 1400:1433 -d mcr.microsoft.com/mssql/server:2017-latest

Run migration from .NET Core web app

Create new web app.

dotnet --version
3.0.100

dotnet new web -o aspnetcore-sample
cd aspnetcore-sample

Add Yuniql.AspNetCore.

dotnet add package Yuniql.AspNetCore
dotnet build

Copy sample database into _db directory in your project.

git clone https://github.com/rdagumampan/yuniql.git c:\temp\yuniql
cd c:\temp\yuniql\samples\basic-sqlserver-sample

Modify the Configure method of Startup.cs, add these lines.

using Yuniql.AspNetCore;
...
...

//create custom trace message sinks, this can be your own logger framework
var traceService = new ConsoleTraceService { IsDebugEnabled = true };

//run migrations until latest version 
app.UseYuniql(traceService, new Yuniql.AspNetCore.Configuration
{
	Platform = "sqlserver",
	Workspace = Path.Combine(Environment.CurrentDirectory, "_db"),
	ConnectionString = "Server=localhost,1400;Database=helloyuniql;User Id=SA;Password=P@ssw0rd!",
	IsAutoCreateDatabase = true, IsDebug = true
});

Test run.

dotnet build
dotnet run --debug

A working sample is available here for your reference https://github.com/rdagumampan/yuniql/tree/master/samples/sqlserver-aspnetcore-sample.

Learn further

Found bugs?

Help us improve further please create an issue.

Comments