Skip to content

Commit df82833

Browse files
committed
Allow new partial templates to be regisgered.
Thanks to @omatrot, @gejotas, @TryAndErr0r (Mehmke Adrian), Gert-Jan ter Schure.
1 parent e7ec43d commit df82833

28 files changed

+579
-177
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ Before creating a pull request, please refer to the [Contributing Guidelines](ht
7070
specific interfaces.
7171
- When you run the _dotnet-ef-dbcontext-scaffold_ command again, you will see your updated reflected in the generated classes.
7272

73+
## Additional Partial Templates
74+
75+
You can add new partial templates to one of the `Partials` folders, then reference with the usual Handlebars syntax. For example, a file named **Comment.hbs** can be referenced from **Class.hbs**.
76+
77+
```hbs
78+
{{{> comment}}}
79+
public class {{class}}
80+
{
81+
```
82+
7383
## Excluded Tables
7484

7585
You can optionally exclude certain tables from code generation. These may also be qualified by schema name.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{{> dbimports}}
2+
3+
namespace {{namespace}}
4+
{ // Comment
5+
public partial class {{class}} : DbContext
6+
{
7+
{{{> dbsets}}}
8+
{{#if entity-type-errors}}
9+
{{#each entity-type-errors}}
10+
{{spaces 8}}{{{entity-type-error}}}
11+
{{/each}}
12+
13+
{{/if}}
14+
15+
{{{> dbconstructor}}}
16+
17+
{{{> dbonconfiguring}}}
18+
19+
{{{on-model-creating}}}
20+
}
21+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{{spaces 8}}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2+
{{spaces 8}}{
3+
{{spaces 12}}if (!optionsBuilder.IsConfigured)
4+
{{spaces 12}}{
5+
{{#if suppress-connectionstring-warning}}
6+
{{spaces 16}}{{sensitive-information-warning}}
7+
{{/if}}
8+
{{spaces 16}}optionsBuilder{{options-builder-provider}};
9+
{{spaces 11}} }
10+
{{spaces 7}} }

sample/ScaffoldingSample.TypeScript/ScaffoldingSample.TypeScript.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77

88
<ItemGroup>
99
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.0" />
10-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0">
11-
<PrivateAssets>all</PrivateAssets>
12-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
13-
</PackageReference>
10+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.0" />
1411
</ItemGroup>
1512

1613
<ItemGroup>

sample/ScaffoldingSample/CodeTemplates/CSharpDbContext/DbContext.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace {{namespace}}
1414

1515
{{{> dbconstructor}}}
1616

17+
{{{> dbonconfiguring}}}
18+
1719
{{{on-model-creating}}}
1820
}
1921
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{{on-configuring}}}

sample/ScaffoldingSample/CodeTemplates/CSharpEntityType/Class.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{{> imports}}
22

33
namespace {{namespace}}
4-
{ // Comment
4+
{ {{{> comment}}}
55
{{#if class-annotation}}
66
{{spaces 4}}{{{class-annotation}}}
77
{{/if}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Comment

sample/ScaffoldingSample/Contexts/NorthwindSlimContext.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public NorthwindSlimContext(DbContextOptions<NorthwindSlimContext> options) : ba
2626
{
2727
}
2828

29+
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
30+
{
31+
if (!optionsBuilder.IsConfigured)
32+
{
33+
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
34+
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB; Initial Catalog=NorthwindSlim; Integrated Security=True");
35+
}
36+
}
37+
38+
2939
protected override void OnModelCreating(ModelBuilder modelBuilder)
3040
{
3141
modelBuilder.Entity<Category>(entity =>
@@ -37,6 +47,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3747

3848
modelBuilder.Entity<Customer>(entity =>
3949
{
50+
entity.HasComment("hello table Customer");
51+
4052
entity.Property(e => e.CustomerId)
4153
.HasMaxLength(5)
4254
.IsFixedLength();
@@ -45,7 +57,8 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4557

4658
entity.Property(e => e.CompanyName)
4759
.IsRequired()
48-
.HasMaxLength(40);
60+
.HasMaxLength(40)
61+
.HasComment("hello CompanyName");
4962

5063
entity.Property(e => e.ContactName).HasMaxLength(30);
5164

sample/ScaffoldingSample/ScaffoldingDesignTimeServices.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using EntityFrameworkCore.Scaffolding.Handlebars;
56
using HandlebarsDotNet;
@@ -19,6 +20,9 @@ public class ScaffoldingDesignTimeServices : IDesignTimeServices
1920
{
2021
public void ConfigureDesignTimeServices(IServiceCollection services)
2122
{
23+
// Uncomment to launch JIT debugger and hit breakpoints
24+
//Debugger.Launch();
25+
2226
// Add Handlebars scaffolding templates
2327
services.AddHandlebarsScaffolding(options =>
2428
{

0 commit comments

Comments
 (0)