efup 0.0.2

dotnet tool install --global efup --version 0.0.2                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local efup --version 0.0.2                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=efup&version=0.0.2                
nuke :add-package efup --version 0.0.2                

EFUP: Database migrations deployment + EF Core context/entities generation

Inspired by DbUp and EntityFrameworkCore.Generator, but radically simplified and reworked with the following changes/principles:

  • Don't make me think - the tool is self-documenting and easy to use.
  • No Yaml, the configuration is more restricted/opinionated and done mostly via command line options.
  • All generated files are located relative to a single target directory.
  • The target directory is fully cleaned and recreated on generation. The generated classes are partial and have hooks for manual customizations that live outside the target directory.
  • No view models/mapping/validators/query extensions/foreign keys (because YAGNI).
  • Optional System.Text.Json annotations;
  • Optional attribute annotations for generated entities;
  • Optional grouping of entities by schema.

Installation & usage

To install EFUP tool run the following command in the console

dotnet tool install --global efup

After the installation the efup command line tool will be available. Run efup --help to see available options and usage examples.

efup --help

Usage: efup [options]

Options:
  --version                                  Show version information.
  --help                                     Show help information.
  -p|--provider <Provider>                   A database provider. One of `SqlServer` (alias `MSSQL`/`ms`), `PostgreSQL` (`Postgres`/`pg`), `MySQL` (`my`) or `Sqlite` (case insensitive).
  -c|--connection-string <ConnectionString>  Database connection string to reverse engineer.
  -w|--working-dir <WorkingDirectory>        A working directory override. By default it is the current directory.
                                             Default value is: D:\Dev\EfUp\artifacts\Debug\net6.0.
  -d|--data-dir <DataDir>                    A directory inside the working directory where database context and entities are generated.
                                             Default value is: EFUP.
  -m|--migrations-dir <MigrationsDir>        A directory inside the working directory with SQL migration files. If this option is missing we use the directory 'DataDir/SQL' but only if it is    
                                             already present.
  -n|--namespace <Namespace>                 Project namespace.
  -u|--secrets-id <UserSecretsId>            A UserSecretsId to use.
  -k|--secrets-key <UserSecretKey>           A user secret key for a connection string.
  -g|--group-by-schema                       Group entities by schema.
  -x|--exclude-default-schema                Exclude the default schema, e.g. `dbo` for MSSQL or `public` for PG.
  -j|--json                                  Add JSON attributes to entities.
  -a|--attributes                            Use EF attribute annotations where possible.
  --schema-prefix                            Add schema name prefix to entity names.
  --strict                                   Enable strict mode: no unused configs or unknown schemas/tables.
  --csproj                                   Generate a .csproj if it's missing in the working directory.

Examples:

In the current working directory `WD` with a database data.db apply migrations from `WD/SQLMigrations` and generate EF context and entities in `WD/DbContextAndEntities`:

> efup -p sqlite -d DbContextAndEntities -m SQLMigrations -c "Data Source=data.db;"

Save connection string for future usage by a key. The presence of both a connection string `-c` and a secrets key `-k` overwites the key's value with the connection string. Other parameters are ignored. The `-i` parameter for UserSecretsId is optional. When it is missing the default shared key `EFUPSharedUserSecretsId` is used.

> efup -i MyUserSecretsId -k my_sqlite_conn_dev -c "Data Source=data.db;"

Use the secrets key instead of connection string:

> efup -p sqlite -d DbContextAndEntities -m SQLMigrations -i MyUserSecretsId -k my_sqlite_conn_dev



Run on build

You could run EFUP tool automatically before project build by adding this section to a .csproj file:

If you install EFUP as a global tool:

  <Target Name="RunEFUP" BeforeTargets="Build">
    <Exec Command='efup -p sqlite -d Sakila -c "Data Source=sakila_master.db" -w "$(ProjectDir.TrimEnd("\")) " -g -a -j -m SQL'
          ContinueOnError="WarnAndContinue"
          WorkingDirectory="$(ProjectDir)"/>
  </Target>

If you install efup as a local tool:

  <Target Name="RestoreTools" BeforeTargets="RunEFUP">
    <Exec Command="dotnet tool restore"/>
  </Target>

  <Target Name="RunEFUP" BeforeTargets="Build">
    <Exec Command='dotnet tool run efup -p sqlite -d Sakila -c "Data Source=sakila_master.db" -w "$(ProjectDir.TrimEnd("\")) " -g -a -j -m SQL'
          ContinueOnError="WarnAndContinue"
          WorkingDirectory="$(ProjectDir)"/>
  </Target>

See an example project here.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
0.0.2 593 10/22/2023
0.0.1 371 10/2/2023