Scaffold EF Core models using Handlebars templates.
- Uses Handlebars.NET to compile Handlebars templates when generating models with the Entity Framework Core scaffolding tools.
- Visual Studio 2017 15.3 or greater.
- The .NET Core 2.0 SDK
- 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.
-
Create a new .NET Core class library.
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.
Open the Package Manager Console, select the default project and enter:Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
-
Edit the .csproj file to add the following section:
<ItemGroup> <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" /> </ItemGroup>
- Open a command prompt at the project location and run:
dotnet restore
.
- Open a command prompt at the project location and run:
-
Add the EntityFrameworkCore.Scaffolding.Handlebars NuGet package:
Install-Package EntityFrameworkCore.Scaffolding.Handlebars -Pre
-
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 and use the EF .NET 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
- 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.