Skip to content
Olusegun Akinyelure edited this page Jan 9, 2023 · 3 revisions

Introduction

DbUpdater.EFCore.CLI was developed as a need to include database migrations, data seed or reference data, and script execution into the continuous integration and deployment pipeline. This prevents the need to create extra support or DBA tickets to migrate code-first entities into the production database or the need for a dev to do those migrations directly into production. (Follow link for more reading) DbUpdater.EFCore.CLI helps you achieve the following

  • Inclusion of data migrations into the CI/CD process
  • Avoid performance issues related to running migrations at runtime
  • Keep migration scripts in the same codebase. Easy for tracking to see how entities and data architecture have changed so far. For companies with the need for SOC2 compliance, this is the best solution. You get to have oversight of every script ever executed in the database.
  • Enables developers to fine-tune migration scripts for performance or other need
  • Enables developers to deploy script as code

RunTime Support

DbUpdater.EFCore.CLI is only supported apps that runs on the following runtime

  • .NET 5.0
  • .NET 6.0
  • .NET 7.0

Because of the difference between the EF Core version of .NET Core 3.1 and below, this library will not work in those applications. .NET framework applications should use the DbUpdater.EF.CLI version.

Set up

Install via .NET CLI

dotnet add package DbUpdater.EFCore.CLI

Install via Package Manager

Install-Package DbUpdater.EFCore.CLI DbUpdater.EFCore.CLI is dependent on the Entity Framework Core library using the SQL provider. You will need to have a EFCore context first and the context must be injected into the service collection so that DbUpdater.EFCore.CLI can pick it up

Add CLI support to Program.cs file

using DbUpdater.EFCore.CLI;
/// The args is the argument passed to the Main method
app.DeployMigrationFromCLI(args);

Add DbContext to Service Collection

services.AddDbContext<MyDbContext>(c => c.UseSqlServer("connection string"));

This CLI support only works on already published source code. This documentation assumes you have deployed the application into a location accessible to you. This could be Local, any other environment, from source artifact from CI/CD or even docker.

Execute Deployment via Powershell

dotnet {MY_ASSEMBLY}.dll -m -s -S create-table.sql scripts/delete.sproc.sql -c {FullContextName}

Execute Deployment via CMD

{MY_ASSEMBLY}.exe -m -s -S create-table.sql scripts/delete.sproc.sql -c {FullContextName}

Arguments / Options

  • -h, --help will display all the options that can be used for the migration
  • -m, --migrate Defaults to false. When set, will tell the DBUpdater to persist the migrations to the database
  • -s, --seed Defaults to false. When set, will tell the DBUpdater to persist the seed data to the database
  • -c, --context The full name of the data contexts. The full name it the assembly name and the data context class. Example is DbUpdater.Web.DbUpdaterContext DbUpdater.Web is the assembly while DbUpdaterContext is the DbContext which will usually extend the EFCore's DbContext class
  • -S, --scripts A list of SQL scripts to be executed.