EntityFrameworkCore.Scaffolding.Handlebars
3.0.0-preview8
See the version list below for details.
dotnet add package EntityFrameworkCore.Scaffolding.Handlebars --version 3.0.0-preview8
NuGet\Install-Package EntityFrameworkCore.Scaffolding.Handlebars -Version 3.0.0-preview8
<PackageReference Include="EntityFrameworkCore.Scaffolding.Handlebars" Version="3.0.0-preview8" />
paket add EntityFrameworkCore.Scaffolding.Handlebars --version 3.0.0-preview8
#r "nuget: EntityFrameworkCore.Scaffolding.Handlebars, 3.0.0-preview8"
// Install EntityFrameworkCore.Scaffolding.Handlebars as a Cake Addin #addin nuget:?package=EntityFrameworkCore.Scaffolding.Handlebars&version=3.0.0-preview8&prerelease // Install EntityFrameworkCore.Scaffolding.Handlebars as a Cake Tool #tool nuget:?package=EntityFrameworkCore.Scaffolding.Handlebars&version=3.0.0-preview8&prerelease
Entity Framework Core Scaffolding with Handlebars
Scaffold EF Core models using Handlebars templates.
- Uses Handlebars.NET to compile Handlebars templates when generating models with the Entity Framework Core scaffolding tools.
Prerequisites
- Visual Studio 2019 16.3 or greater.
- The .NET Core 3.0 SDK.
Database Setup
- 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.
- The easiest is to use LocalDb, which is installed with Visual Studio.
Usage
Create a new .NET Core class library.
- If necessary, edit the csproj file to update the TargetFramework to 3.0.
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.
Add EF Core SQL Server and Tools NuGet packages.
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Design
Add the EntityFrameworkCore.Scaffolding.Handlebars NuGet package:
EntityFrameworkCore.Scaffolding.Handlebars
Remove Class1.cs and add a ScaffoldingDesignTimeServices class.
- Implement
IDesignTimeServices
by adding aConfigureDesignTimeServices
method that callsservices.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); } }
- Implement
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.
- Get help on dotnet-ef-dbcontext-scaffold at the command line:
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 and Transformers
You can register Handlebars helpers in the ScaffoldingDesignTimeServices
where setup takes place.
- Create a named tuple as shown with
myHelper
below. - The
context
parameter of the helper method provides model data injected by the Handlebars scaffolding extension. - Pass the tuple to the
AddHandlebarsHelpers
extension method. - To use Handlebars helper defined above, add the following to any of the .hbs files within the CodeTemplates folder:
{{my-helper}}
- You may register as many helpers as you wish.
You can pass transform functions to AddHandlebarsTransformers
in order to customize generation of entity type definitions, including class names, constructors and properties.
public class ScaffoldingDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection services)
{
// Generate both context and entities
var options = ReverseEngineerOptions.DbContextAndEntities;
// Register Handlebars helper
var myHelper = (helperName: "my-helper", helperFunction: (Action<TextWriter, Dictionary<string, object>, object[]>) MyHbsHelper);
// Add Handlebars scaffolding templates
services.AddHandlebarsScaffolding(options);
// Add optional Handlebars helpers
services.AddHandlebarsHelpers(myHelper);
// Add Handlebars transformer for Country property
services.AddHandlebarsTransformers(
propertyTransformer: e =>
e.PropertyName == "Country"
? new EntityPropertyInfo("Country", e.PropertyName)
: new EntityPropertyInfo(e.PropertyType, e.PropertyName));
// Add optional Handlebars transformers
//services.AddHandlebarsTransformers(
// entityNameTransformer: n => n + "Foo",
// entityFileNameTransformer: n => n + "Foo",
// constructorTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"),
// propertyTransformer: e => new EntityPropertyInfo(e.PropertyType, e.PropertyName + "Foo"),
// navPropertyTransformer: e => new EntityPropertyInfo(e.PropertyType + "Foo", e.PropertyName + "Foo"));
}
// Sample Handlebars helper
void MyHbsHelper(TextWriter writer, Dictionary<string, object> context, object[] parameters)
{
writer.Write("// My Handlebars Helper");
}
}
Extending the OnModelCreating Method
There are times when you might like to modify generated code, for example, by adding a HasConversion
method to an entity property in the OnModelCreating
method of the generated class that extends DbContext
. However, doing so may prove futile because added code would be overwritten the next time you run the dotnet ef dbcontext scaffold
command.
Rather than modifying generated code, a better idea would be to extend it by using partial classes and methods. To enable this scenario, the generated
DbContext
class is already defined using thepartial
keyword, and it contains a partialOnModelCreatingPartial
method that is invoked at the end of theOnModelCreating
method.To implement the partial method, simply add a new class to your project with the same name as the generated
DbContext
class, and define it aspartial
. Then add aOnModelCreatingPartial
method with the same signature as the partial method defined in the generatedDbContext
class.Install the global
dotnet ef
tool.
dotnet tool install --global dotnet-ef --version 3.0.0-*
- Open a command prompt at the project root and execute:
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
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Handlebars.Net (>= 1.10.1)
- JetBrains.Annotations (>= 2019.1.3)
- Microsoft.EntityFrameworkCore.Design (>= 3.0.0-preview8.19405.11)
NuGet packages (6)
Showing the top 5 NuGet packages that depend on EntityFrameworkCore.Scaffolding.Handlebars:
Package | Downloads |
---|---|
TrueSight
Package Description |
|
TrueSight.Lite
Package Description |
|
ODS_Fantasy
Package Description |
|
Neyland
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 in Visual Studio & CLI
|
Version | Downloads | Last updated |
---|---|---|
8.0.0 | 127,512 | 1/27/2024 |
8.0.0-beta2 | 631 | 12/30/2023 |
8.0.0-beta1 | 1,683 | 11/23/2023 |
7.0.0 | 96,153 | 6/12/2023 |
7.0.0-beta1 | 19,326 | 11/20/2022 |
6.0.3 | 396,072 | 1/22/2022 |
6.0.2 | 10,873 | 12/23/2021 |
6.0.1 | 1,431 | 12/21/2021 |
6.0.0 | 6,770 | 12/15/2021 |
6.0.0-preview5 | 208 | 12/15/2021 |
6.0.0-preview4 | 289 | 12/5/2021 |
6.0.0-preview3 | 4,774 | 11/30/2021 |
6.0.0-preview2 | 3,609 | 11/25/2021 |
6.0.0-preview1 | 3,390 | 11/25/2021 |
5.0.5-preview1 | 8,062 | 9/25/2021 |
5.0.4 | 73,869 | 9/23/2021 |
5.0.3 | 13,001 | 9/15/2021 |
5.0.2 | 89,301 | 2/19/2021 |
5.0.1 | 21,615 | 12/5/2020 |
5.0.0 | 2,337 | 11/15/2020 |
5.0.0-rc.2 | 711 | 10/18/2020 |
3.8.5 | 28,133 | 11/7/2020 |
3.8.4 | 14,952 | 9/28/2020 |
3.8.3 | 96,452 | 8/18/2020 |
3.8.2 | 3,548 | 7/21/2020 |
3.8.1 | 7,856 | 7/8/2020 |
3.7.0 | 44,205 | 5/1/2020 |
3.6.0 | 21,618 | 1/6/2020 |
3.5.1 | 2,280 | 12/15/2019 |
3.5.0 | 6,211 | 10/18/2019 |
3.0.0 | 1,212 | 10/14/2019 |
3.0.0-preview8 | 464 | 8/26/2019 |
2.1.1 | 36,103 | 8/5/2019 |
2.1.0 | 1,365 | 7/19/2019 |
2.0.0 | 5,915 | 6/25/2019 |
1.7.2 | 119,871 | 1/6/2019 |
1.7.1 | 979 | 1/5/2019 |
1.7.0 | 765 | 1/3/2019 |
1.6.0 | 1,406 | 12/17/2018 |
1.5.1 | 25,560 | 7/7/2018 |
1.5.0 | 1,234 | 6/22/2018 |
1.4.1 | 1,174 | 7/7/2018 |
1.4.0 | 1,053 | 6/23/2018 |
1.1.1 | 1,098 | 6/19/2018 |
1.1.0 | 1,182 | 6/3/2018 |
1.0.0 | 1,209 | 5/30/2018 |
1.0.0-rc3 | 1,076 | 5/26/2018 |
1.0.0-rc2 | 850 | 5/26/2018 |
1.0.0-rc | 839 | 5/24/2018 |
1.0.0-beta | 2,563 | 10/25/2017 |