EntityFrameworkCore.Scaffolding.Handlebars 1.0.0-rc3

.NET Standard 2.0
This is a prerelease version of EntityFrameworkCore.Scaffolding.Handlebars.
There is a newer version of this package available.
See the version list below for details.
dotnet add package EntityFrameworkCore.Scaffolding.Handlebars --version 1.0.0-rc3
NuGet\Install-Package EntityFrameworkCore.Scaffolding.Handlebars -Version 1.0.0-rc3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCore.Scaffolding.Handlebars" Version="1.0.0-rc3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.Scaffolding.Handlebars --version 1.0.0-rc3
#r "nuget: EntityFrameworkCore.Scaffolding.Handlebars, 1.0.0-rc3"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCore.Scaffolding.Handlebars as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.Scaffolding.Handlebars&version=1.0.0-rc3&prerelease

// Install EntityFrameworkCore.Scaffolding.Handlebars as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.Scaffolding.Handlebars&version=1.0.0-rc3&prerelease

Entity Framework Core Scaffolding with Handlebars

Scaffold EF Core models using Handlebars templates.

Prerequisites

Database Setup

  1. Use SQL Server Management Studio to connect to SQL Server
    • The easiest is to use LocalDb, which is installed with Visual Studio.
      Connect to: (localdb)\MsSqlLocalDb.
    • Create a new database named NorthwindSlim.
    • Download the data file from http://bit.ly/northwindslim.
    • Unzip NorthwindSlim.sql and run the script to create tables and populate them with data.

Usage

  1. Create a new .NET Core class library.

    • If necessary, edit the csproj file to update the TargetFramework to 2.1.

    Note: Using the EF Core toolchain with a .NET Standard class library is currently not supported. Instead, you can add a .NET Standard class library to the same solution as the .NET Core library, then add existing items and select Add As Link to include entity classes.

  2. Add EF Core SQL Server and Tools NuGet packages.

    • Open the Package Manager Console, select the default project and enter:
      • Install-Package Microsoft.EntityFrameworkCore.SqlServer
    • If needed update EF Core to version 2.1.
  3. Add the EntityFrameworkCore.Scaffolding.Handlebars NuGet package:

    • Install-Package EntityFrameworkCore.Scaffolding.Handlebars -Pre
  4. Remove Class1.cs and add a ScaffoldingDesignTimeServices class.

    • Implement IDesignTimeServices by adding a ConfigureDesignTimeServices method that calls services.AddHandlebarsScaffolding.
    • You can optionally pass a ReverseEngineerOptions enum to indicate if you wish to generate only entity types, only a DbContext class, or both (which is the default).
    public class ScaffoldingDesignTimeServices : IDesignTimeServices
    {
        public void ConfigureDesignTimeServices(IServiceCollection services)
        {
            var options = ReverseEngineerOptions.DbContextAndEntities;
            services.AddHandlebarsScaffolding(options);
        }
    }
    
  5. Open a command prompt at the project level and use the EF .NET Core CLI tools to reverse engineer a context and models from an existing database.

    • Get help on dotnet-ef-dbcontext-scaffold at the command line: dotnet ef dbcontext scaffold -h
    • Execute the following command to reverse engineer classes from the NorthwindSlim database:
    dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True" Microsoft.EntityFrameworkCore.SqlServer -o Models -c NorthwindSlimContext -f --context-dir Contexts
    
    • You should see context and/or entity classes appear in the Models folder of the project.
    • You will also see a CodeTemplates folder appear containing Handlebars templates for customizing generation of context and entity type classes.
    • Add -d to the command to use data annotations. You will need to add the System.ComponentModel.Annotations package to a .NET Standard library containing linked entity classes.
  6. You may edit any of the template files which appear under the CodeTemplates folder.

    • For now you can just add some comments, but you may wish to customize the templates in other ways, for example, by inheriting entities from a base class or implementing specific interfaces.
    • When you run the dotnet-ef-dbcontext-scaffold command again, you will see your updated reflected in the generated classes.

Handlebars Helpers

You can register Handlebars helpers in the ScaffoldingDesignTimeServices where setup takes place.

  • Create a named tuple as shown with myHelper below.
  • Pass the tuple to the AddHandlebarsScaffolding extension method.
  • You may register as many helpers as you wish.
public class ScaffoldingDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection services)
    {
        // Generate both context and entitites
        var options = ReverseEngineerOptions.DbContextAndEntities;

        // Register Handlebars helper
        var myHelper = (helperName: "my-helper", helperFunction: (Action<TextWriter, object, object[]>) MyHbsHelper);

        // Add Handlebars scaffolding templates
        services.AddHandlebarsScaffolding(options, myHelper);
    }

    // Sample Handlebars helper
    void MyHbsHelper(TextWriter writer, object context, object[] parameters)
    {
        writer.Write("// My Handlebars Helper");
    }
}
  • To use Handlebars helper defined above, add the following to any of the .hbs files within the CodeTemplates folder: {{my-helper}}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on EntityFrameworkCore.Scaffolding.Handlebars:

Package Downloads
TrueSight

Package Description

TrueSight.Lite

Package Description

TrueSight.Lite.Net5

Package Description

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EntityFrameworkCore.Scaffolding.Handlebars:

Repository Stars
ErikEJ/EFCorePowerTools
Entity Framework Core Power Tools - reverse engineering, migrations and model visualization for EF Core
Version Downloads Last updated
7.0.0-beta1 1,594 11/20/2022
6.0.3 130,753 1/22/2022
6.0.2 7,112 12/23/2021
6.0.1 1,271 12/21/2021
6.0.0 1,473 12/15/2021
6.0.0-preview5 110 12/15/2021
6.0.0-preview4 184 12/5/2021
6.0.0-preview3 2,164 11/30/2021
6.0.0-preview2 3,309 11/25/2021
6.0.0-preview1 3,294 11/25/2021
5.0.5-preview1 1,572 9/25/2021
5.0.4 31,885 9/23/2021
5.0.3 10,455 9/15/2021
5.0.2 65,743 2/19/2021
5.0.1 17,931 12/5/2020
5.0.0 2,116 11/15/2020
5.0.0-rc.2 648 10/18/2020
3.8.5 25,895 11/7/2020
3.8.4 14,729 9/28/2020
3.8.3 66,310 8/18/2020
3.8.2 3,350 7/21/2020
3.8.1 5,780 7/8/2020
3.7.0 39,545 5/1/2020
3.6.0 16,881 1/6/2020
3.5.1 2,138 12/15/2019
3.5.0 4,130 10/18/2019
3.0.0 993 10/14/2019
3.0.0-preview8 400 8/26/2019
2.1.1 31,345 8/5/2019
2.1.0 1,236 7/19/2019
2.0.0 4,991 6/25/2019
1.7.2 71,547 1/6/2019
1.7.1 814 1/5/2019
1.7.0 605 1/3/2019
1.6.0 1,199 12/17/2018
1.5.1 20,409 7/7/2018
1.5.0 1,066 6/22/2018
1.4.1 1,000 7/7/2018
1.4.0 874 6/23/2018
1.1.1 926 6/19/2018
1.1.0 998 6/3/2018
1.0.0 869 5/30/2018
1.0.0-rc3 940 5/26/2018
1.0.0-rc2 709 5/26/2018
1.0.0-rc 711 5/24/2018
1.0.0-beta 2,401 10/25/2017