ef-migration-runtime-schema
1.0.0
See the version list below for details.
dotnet tool install --global ef-migration-runtime-schema --version 1.0.0
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local ef-migration-runtime-schema --version 1.0.0
#tool dotnet:?package=ef-migration-runtime-schema&version=1.0.0
nuke :add-package ef-migration-runtime-schema --version 1.0.0
ef-migration-runtime-schema
CLI to add constuctor injection after EF Migration to allow customization of the schema at runtime
This projet was motivated by the need to specify a custom schema during a runtime migration when using Entity Framework.
When you use the EF Core CLI to create migration, this will create different files as mention at https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/managing?tabs=dotnet-core-cli.
The migration file will contains a class with up
and down
methods regarding the change to apply to the database.
Sample for up
migrationBuilder.AddColumn<string>(
name: "FullName",
schema: "mySchemaName",
table: "Customers",
nullable: true);
Sample for down
migrationBuilder.DropColumn(
schema: "mySchemaName",
name: "Name",
table: "Customers");
as you can see the schema name is include and fixed in the code to a constant value.
After some search, I've seen this very good blog post that allow you to change the generated migration file to allow to configure the schema name at runtime.
Briefly, the idea is to :
- add a constructor to the Migration class generated and inject an IDbContextSchema interface to get the schema name
- add a new class that inherit from MigrationsAssembly to allow the usage of Migration class that have a constructor with the IDbContextSchema interface
This allow you to use migration at runtime with a specific schema BUT you always have to modify your migration class generated by the EF Core CLI.
The idea of this tool is to automated the change in the generated file.
This tool will :
- use ef core cli to generate the migration file
- open the migration file
- add constructor / private field
- change the schema value by the property from the private field.
The tool can be launch in two way :
- using the path to the generated file :
ef-migration-runtime-schema --interface FullNamespace.Interface --migrationFile PathToTheMigrationFile
- using the ef options, when generating a new migration
ef-migration-runtime-schema --interface FullNamespace.Interface --efOptions "the ef command options"
if you basic ef command is dotnet ef migrations add AddUserName -o /migrationPath
, you efoptions will be migrations add AddUserName -o /migrationPath
The interface should respect the following :
interface ISchemaInterface
{
/// <summary>
/// Name of the Schema
/// </summary>
public string Schema { get; }
}
Indeed the cli will add the following code in the migration file :
public partial class AddUserName : Migration
{
-> private readonly FullNamespace.Interface _schema;
->
-> /// <inheritdoc />
-> public AddUserName(FullNamespace.Interface schema)
-> {
-> _schema = schema;
-> }
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "FullName",
-> schema: _schema.Schema,
table: "Customers",
nullable: true);
}
}
The full CLI Help is the following :
Description:
CLI for ef-miration-runtime-schema
Usage:
ef-migration-runtime-schema [options]
Options:
--efOptions <efoptions> ef command line command and arguments
--interface <interface> (REQUIRED) The name of the interface to inject
--migrationFile <migrationFile> The Path to the migration file
--version Show version information
-?, -h, --help Show help and usage information
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
This package has no dependencies.