Skip to content

Commit 7cf07c5

Browse files
authored
Merge pull request #127 from TrackableEntities/dbcontext-onconfig-template
Add DbConfiguring.hbs Partial Template
2 parents 62117ec + 05dd028 commit 7cf07c5

File tree

23 files changed

+179
-56
lines changed

23 files changed

+179
-56
lines changed
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
{{{on-configuring}}}
1+
{{spaces 8}}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2+
{{spaces 8}}{
3+
{{spaces 12}}if (!optionsBuilder.IsConfigured)
4+
{{spaces 12}}{
5+
{{connectionstring-warning}}
6+
{{spaces 16}}optionsBuilder{{options-builder-provider}};
7+
{{spaces 11}} }
8+
{{spaces 7}} }

sample/ScaffoldingSample/Contexts/NorthwindSlimContext.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
3535
}
3636
}
3737

38-
3938
protected override void OnModelCreating(ModelBuilder modelBuilder)
4039
{
4140
modelBuilder.Entity<dbo.Category>(entity =>

src/EntityFrameworkCore.Scaffolding.Handlebars/CodeTemplates/CSharpDbContext/DbContext.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace {{namespace}}
1414

1515
{{{> dbconstructor}}}
1616

17-
{{{on-configuring}}}
17+
{{{> dbonconfiguring}}}
18+
1819
{{{on-model-creating}}}
1920
}
2021
}
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 connectionstring-warning}}
6+
{{connectionstring-warning}}
7+
{{/if}}
8+
{{spaces 16}}optionsBuilder{{options-builder-provider}};
9+
{{spaces 11}} }
10+
{{spaces 7}} }

src/EntityFrameworkCore.Scaffolding.Handlebars/CodeTemplates/TypeScriptDbContext/DbContext.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace {{namespace}}
1414

1515
{{{> dbconstructor}}}
1616

17-
{{{on-configuring}}}
17+
{{{> dbonconfiguring}}}
18+
1819
{{{on-model-creating}}}
1920
}
2021
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{spaces 8}}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2+
{{spaces 8}}{
3+
{{spaces 12}}if (!optionsBuilder.IsConfigured)
4+
{{spaces 12}}{
5+
{{connectionstring-warning}}
6+
{{spaces 16}}optionsBuilder{{options-builder-provider}};
7+
{{spaces 11}} }
8+
{{spaces 7}} }

src/EntityFrameworkCore.Scaffolding.Handlebars/EntityFrameworkCore.Scaffolding.Handlebars.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@
2929
<DocumentationFile>bin\Release\netstandard2.1\EntityFrameworkCore.Scaffolding.Handlebars.xml</DocumentationFile>
3030
</PropertyGroup>
3131

32+
<ItemGroup>
33+
<None Remove="CodeTemplates\CSharpDbContext\Partials\DbOnConfiguring.hbs" />
34+
</ItemGroup>
35+
3236
<ItemGroup>
3337
<None Include="icon.png" Pack="true" Visible="false" PackagePath="" />
3438
</ItemGroup>
3539

3640
<ItemGroup>
3741
<!-- pack tempate files -->
3842
<Content Include="CodeTemplates\CSharpDbContext\DbContext.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\" />
43+
<Content Include="CodeTemplates\CSharpDbContext\Partials\DbOnConfiguring.hbs">
44+
<PackagePath>lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\</PackagePath>
45+
</Content>
46+
<Content Include="CodeTemplates\TypeScriptDbContext\Partials\DbOnConfiguring.hbs">
47+
<PackagePath>lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\</PackagePath>
48+
</Content>
3949
<Content Include="CodeTemplates\CSharpDbContext\Partials\DbImports.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\" />
4050
<Content Include="CodeTemplates\CSharpDbContext\Partials\DbConstructor.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\" />
4151
<Content Include="CodeTemplates\CSharpDbContext\Partials\DbSets.hbs" PackagePath="lib\netstandard2.1\CodeTemplates\CSharpDbContext\Partials\" />

src/EntityFrameworkCore.Scaffolding.Handlebars/HbsCSharpDbContextGenerator.cs

Lines changed: 63 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -164,55 +164,21 @@ protected override void GenerateOnConfiguring(string connectionString, bool supp
164164
{
165165
Check.NotNull(connectionString, nameof(connectionString));
166166

167-
var sb = new IndentedStringBuilder();
168-
using (sb.Indent())
169-
using (sb.Indent())
170-
{
171-
sb.AppendLine("protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)");
172-
sb.AppendLine("{");
173-
174-
using (sb.Indent())
175-
{
176-
sb.AppendLine("if (!optionsBuilder.IsConfigured)");
177-
sb.AppendLine("{");
178-
179-
using (sb.Indent())
180-
{
181-
if (!suppressConnectionStringWarning)
182-
{
183-
sb.DecrementIndent()
184-
.DecrementIndent()
185-
.DecrementIndent()
186-
.DecrementIndent()
187-
.AppendLine("#warning " + DesignStrings.SensitiveInformationWarning)
188-
.IncrementIndent()
189-
.IncrementIndent()
190-
.IncrementIndent()
191-
.IncrementIndent();
192-
}
193-
194-
sb.Append("optionsBuilder");
195-
196-
var useProviderCall = ProviderConfigurationCodeGenerator.GenerateUseProvider(
197-
connectionString,
198-
ProviderConfigurationCodeGenerator.GenerateProviderOptions());
199-
var contextOptions = ProviderConfigurationCodeGenerator.GenerateContextOptions();
200-
if (contextOptions != null)
201-
{
202-
useProviderCall = useProviderCall.Chain(contextOptions);
203-
}
167+
var onConfiguring = GetOnConfiguring(connectionString, suppressConnectionStringWarning);
168+
TemplateData.Add("on-configuring", onConfiguring);
204169

205-
sb.Append(CSharpHelper.Fragment(useProviderCall))
206-
.AppendLine(";");
207-
}
208-
sb.AppendLine("}");
209-
}
170+
TemplateData.Add("connection-string", connectionString);
171+
TemplateData.Add("connectionstring-warning", $"#warning {DesignStrings.SensitiveInformationWarning}");
210172

211-
sb.AppendLine("}");
173+
var useProviderCall = ProviderConfigurationCodeGenerator.GenerateUseProvider(
174+
connectionString, ProviderConfigurationCodeGenerator.GenerateProviderOptions());
175+
var contextOptions = ProviderConfigurationCodeGenerator.GenerateContextOptions();
176+
if (contextOptions != null)
177+
{
178+
useProviderCall = useProviderCall.Chain(contextOptions);
212179
}
213180

214-
var onConfiguring = sb.ToString();
215-
TemplateData.Add("on-configuring", onConfiguring);
181+
TemplateData.Add("options-builder-provider", CSharpHelper.Fragment(useProviderCall));
216182
}
217183

218184
/// <summary>
@@ -969,6 +935,58 @@ private void GenerateSequence(ISequence sequence, IndentedStringBuilder sb)
969935
sb.AppendLine(";");
970936
}
971937

938+
private string GetOnConfiguring(string connectionString, bool suppressConnectionStringWarning)
939+
{
940+
var sb = new IndentedStringBuilder();
941+
using (sb.Indent())
942+
using (sb.Indent())
943+
{
944+
sb.AppendLine("protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)");
945+
sb.AppendLine("{");
946+
947+
using (sb.Indent())
948+
{
949+
sb.AppendLine("if (!optionsBuilder.IsConfigured)");
950+
sb.AppendLine("{");
951+
952+
using (sb.Indent())
953+
{
954+
if (!suppressConnectionStringWarning)
955+
{
956+
sb.DecrementIndent()
957+
.DecrementIndent()
958+
.DecrementIndent()
959+
.DecrementIndent()
960+
.AppendLine("#warning " + DesignStrings.SensitiveInformationWarning)
961+
.IncrementIndent()
962+
.IncrementIndent()
963+
.IncrementIndent()
964+
.IncrementIndent();
965+
}
966+
967+
sb.Append("optionsBuilder");
968+
969+
var useProviderCall = ProviderConfigurationCodeGenerator.GenerateUseProvider(
970+
connectionString,
971+
ProviderConfigurationCodeGenerator.GenerateProviderOptions());
972+
var contextOptions = ProviderConfigurationCodeGenerator.GenerateContextOptions();
973+
if (contextOptions != null)
974+
{
975+
useProviderCall = useProviderCall.Chain(contextOptions);
976+
}
977+
978+
sb.Append(CSharpHelper.Fragment(useProviderCall))
979+
.AppendLine(";");
980+
}
981+
sb.AppendLine("}");
982+
}
983+
984+
sb.AppendLine("}");
985+
}
986+
987+
return sb.ToString();
988+
}
989+
972990
private static string GenerateLambdaToKey(
973991
IReadOnlyList<IProperty> properties,
974992
string lambdaIdentifier,

src/EntityFrameworkCore.Scaffolding.Handlebars/Helpers/Constants.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ public static class Constants
3535
/// <summary>DbContext constructor template.</summary>
3636
public const string DbContextCtorTemplate = "DbConstructor";
3737

38+
/// <summary>DbContext OnConfiguring template.</summary>
39+
public const string DbContextOnConfiguringTemplate = "DbOnConfiguring";
40+
3841
/// <summary>DbContext DbSets template.</summary>
3942
public const string DbContextDbSetsTemplate = "DbSets";
4043

test/Scaffolding.Handlebars.Tests/CodeTemplates/CSharpDbContext/DbContext.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace {{namespace}}
1414

1515
{{{> dbconstructor}}}
1616

17-
{{{on-configuring}}}
17+
{{{> dbonconfiguring}}}
18+
1819
{{{on-model-creating}}}
1920
}
2021
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{spaces 8}}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2+
{{spaces 8}}{
3+
{{spaces 12}}if (!optionsBuilder.IsConfigured)
4+
{{spaces 12}}{
5+
{{connectionstring-warning}}
6+
{{spaces 16}}optionsBuilder{{options-builder-provider}};
7+
{{spaces 11}} }
8+
{{spaces 7}} }

test/Scaffolding.Handlebars.Tests/CodeTemplates/TypeScriptDbContext/DbContext.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace {{namespace}}
1414

1515
{{{> dbconstructor}}}
1616

17-
{{{on-configuring}}}
17+
{{{> dbonconfiguring}}}
18+
1819
{{{on-model-creating}}}
1920
}
2021
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{{spaces 8}}protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
2+
{{spaces 8}}{
3+
{{spaces 12}}if (!optionsBuilder.IsConfigured)
4+
{{spaces 12}}{
5+
{{connectionstring-warning}}
6+
{{spaces 16}}optionsBuilder{{options-builder-provider}};
7+
{{spaces 11}} }
8+
{{spaces 7}} }

test/Scaffolding.Handlebars.Tests/CodeTemplatesAlt/CSharpDbContext/DbContext.hbs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace {{namespace}}
1414

1515
{{{> dbconstructor}}}
1616

17-
{{{on-configuring}}}
17+
{{{> dbonconfiguring}}}
18+
1819
{{{on-model-creating}}}
1920
}
2021
}

test/Scaffolding.Handlebars.Tests/EmbeddedResourceTemplateFileServiceTests.ExpectedTemplates.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public partial class {{class}} : DbContext
2121
2222
{{{> dbconstructor}}}
2323
24-
{{{on-configuring}}}
24+
{{{> dbonconfiguring}}}
25+
2526
{{{on-model-creating}}}
2627
}
2728
}

test/Scaffolding.Handlebars.Tests/Fakes/FakeCSharpTemplateLanguageService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo(ITempla
3434
FileName = Constants.DbContextCtorTemplate + Constants.TemplateExtension
3535
}
3636
},
37+
{
38+
Constants.DbContextOnConfiguringTemplate,
39+
new TemplateFileInfo
40+
{
41+
RelativeDirectory = Constants.CSharpTemplateDirectories.DbContextPartialsDirectory,
42+
FileName = Constants.DbContextOnConfiguringTemplate + Constants.TemplateExtension
43+
}
44+
},
3745
{
3846
Constants.DbContextDbSetsTemplate,
3947
new TemplateFileInfo

test/Scaffolding.Handlebars.Tests/Fakes/FakeHbsDbContextTemplateService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ protected override IDictionary<string, string> GetPartialTemplates(
7777
var ctorTemplateFile = FileService.RetrieveTemplateFileContents(
7878
ctorFile.RelativeDirectory, ctorFile.FileName);
7979

80+
DbContextTemplateFiles.TryGetValue(Constants.DbContextOnConfiguringTemplate, out TemplateFileInfo onConfiguringFile);
81+
var onConfiguringTemplateFile = FileService.RetrieveTemplateFileContents(
82+
ctorFile.RelativeDirectory, onConfiguringFile.FileName);
83+
8084
DbContextTemplateFiles.TryGetValue(Constants.DbContextDbSetsTemplate, out TemplateFileInfo propertyFile);
8185
var propertyTemplateFile = FileService.RetrieveTemplateFileContents(
8286
propertyFile.RelativeDirectory, propertyFile.FileName);
@@ -91,6 +95,10 @@ protected override IDictionary<string, string> GetPartialTemplates(
9195
Constants.DbContextCtorTemplate.ToLower(CultureInfo.InvariantCulture),
9296
ctorTemplateFile
9397
},
98+
{
99+
Constants.DbContextOnConfiguringTemplate.ToLower(CultureInfo.InvariantCulture),
100+
onConfiguringTemplateFile
101+
},
94102
{
95103
Constants.DbContextDbSetsTemplate.ToLower(CultureInfo.InvariantCulture),
96104
propertyTemplateFile

test/Scaffolding.Handlebars.Tests/Fakes/FakeTypeScriptTemplateLanguageService.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ public Dictionary<string, TemplateFileInfo> GetDbContextTemplateFileInfo(ITempla
3434
FileName = Constants.DbContextCtorTemplate + Constants.TemplateExtension
3535
}
3636
},
37+
{
38+
Constants.DbContextOnConfiguringTemplate,
39+
new TemplateFileInfo
40+
{
41+
RelativeDirectory = Constants.TypeScriptTemplateDirectories.DbContextPartialsDirectory,
42+
FileName = Constants.DbContextOnConfiguringTemplate + Constants.TemplateExtension
43+
}
44+
},
3745
{
3846
Constants.DbContextDbSetsTemplate,
3947
new TemplateFileInfo

test/Scaffolding.Handlebars.Tests/FileSystemTemplateFileServiceTests.ExpectedTemplates.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ public partial class {{class}} : DbContext
2121
2222
{{{> dbconstructor}}}
2323
24-
{{{on-configuring}}}
24+
{{{> dbonconfiguring}}}
25+
2526
{{{on-model-creating}}}
2627
}
2728
}

test/Scaffolding.Handlebars.Tests/HbsCSharpScaffoldingGeneratorTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public partial class HbsCSharpScaffoldingGeneratorTests
2626
private InputFile ContextClassTemplate { get; }
2727
private InputFile ContextImportsTemplate { get; }
2828
private InputFile ContextCtorTemplate { get; }
29+
private InputFile ContextOnConfiguringTemplate { get; }
2930
private InputFile ContextDbSetsTemplate { get; }
3031
private InputFile EntityClassTemplate { get; }
3132
private InputFile EntityImportsTemplate { get; }
@@ -71,6 +72,12 @@ public HbsCSharpScaffoldingGeneratorTests(NorthwindDbContextFixture fixture)
7172
File = Constants.Templates.ContextCtorFile,
7273
Contents = File.ReadAllText(Path.Combine(contextPartialTemplatesPath, Constants.Templates.ContextCtorFile))
7374
};
75+
ContextOnConfiguringTemplate = new InputFile
76+
{
77+
Directory = contextPartialsVirtualPath,
78+
File = Constants.Templates.ContextOnConfiguringFile,
79+
Contents = File.ReadAllText(Path.Combine(contextPartialTemplatesPath, Constants.Templates.ContextOnConfiguringFile))
80+
};
7481
ContextDbSetsTemplate = new InputFile
7582
{
7683
Directory = contextPartialsVirtualPath,
@@ -397,7 +404,8 @@ public void Save_Should_Write_Context_and_Entity_Files()
397404
private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions options, bool usePluralizer)
398405
{
399406
var fileService = new InMemoryTemplateFileService();
400-
fileService.InputFiles(ContextClassTemplate, ContextImportsTemplate, ContextCtorTemplate, ContextDbSetsTemplate,
407+
fileService.InputFiles(ContextClassTemplate, ContextImportsTemplate,
408+
ContextCtorTemplate, ContextOnConfiguringTemplate, ContextDbSetsTemplate,
401409
EntityClassTemplate, EntityImportsTemplate, EntityCtorTemplate, EntityPropertiesTemplate);
402410

403411
var services = new ServiceCollection()

test/Scaffolding.Handlebars.Tests/HbsTypeScriptScaffoldingGeneratorTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public partial class HbsTypeScriptScaffoldingGeneratorTests
2424
private InputFile ContextClassTemplate { get; }
2525
private InputFile ContextImportsTemplate { get; }
2626
private InputFile ContextCtorTemplate { get; }
27+
private InputFile ContextOnConfiguringTemplate { get; }
2728
private InputFile ContextDbSetsTemplate { get; }
2829
private InputFile EntityClassTemplate { get; }
2930
private InputFile EntityImportsTemplate { get; }
@@ -69,6 +70,12 @@ public HbsTypeScriptScaffoldingGeneratorTests(NorthwindDbContextFixture fixture)
6970
File = Constants.Templates.ContextCtorFile,
7071
Contents = File.ReadAllText(Path.Combine(contextPartialTemplatesPath, Constants.Templates.ContextCtorFile))
7172
};
73+
ContextOnConfiguringTemplate = new InputFile
74+
{
75+
Directory = contextPartialsVirtualPath,
76+
File = Constants.Templates.ContextOnConfiguringFile,
77+
Contents = File.ReadAllText(Path.Combine(contextPartialTemplatesPath, Constants.Templates.ContextOnConfiguringFile))
78+
};
7279
ContextDbSetsTemplate = new InputFile
7380
{
7481
Directory = contextPartialsVirtualPath,
@@ -317,7 +324,8 @@ public void Save_Should_Write_Context_and_Entity_Files()
317324
private IReverseEngineerScaffolder CreateScaffolder(ReverseEngineerOptions options)
318325
{
319326
var fileService = new InMemoryTemplateFileService();
320-
fileService.InputFiles(ContextClassTemplate, ContextImportsTemplate, ContextCtorTemplate, ContextDbSetsTemplate,
327+
fileService.InputFiles(ContextClassTemplate, ContextImportsTemplate,
328+
ContextCtorTemplate, ContextOnConfiguringTemplate, ContextDbSetsTemplate,
321329
EntityClassTemplate, EntityImportsTemplate, EntityCtorTemplate, EntityPropertiesTemplate);
322330

323331
var services = new ServiceCollection()

0 commit comments

Comments
 (0)