diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs index f23f89baa2f..59a83ba50c7 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs @@ -158,7 +158,8 @@ protected virtual void GenerateClass( using (_builder.Indent()) { - GenerateConstructors(contextName); + var generateDefaultConstructor = !suppressOnConfiguring; + GenerateConstructors(contextName, generateDefaultConstructor); GenerateDbSets(model); GenerateEntityTypeErrors(model); if (!suppressOnConfiguring) @@ -179,12 +180,15 @@ protected virtual void GenerateClass( _builder.AppendLine("}"); } - private void GenerateConstructors(string contextName) + private void GenerateConstructors(string contextName, bool generateDefaultConstructor) { - _builder.AppendLine($"public {contextName}()") - .AppendLine("{") - .AppendLine("}") - .AppendLine(); + if (generateDefaultConstructor) + { + _builder.AppendLine($"public {contextName}()") + .AppendLine("{") + .AppendLine("}") + .AppendLine(); + } _builder.AppendLine($"public {contextName}(DbContextOptions<{contextName}> options)") .IncrementIndent() diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs index bc41773e18f..9bf9f8c5a4a 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs @@ -146,10 +146,6 @@ namespace TestNamespace { public partial class TestDbContext : DbContext { - public TestDbContext() - { - } - public TestDbContext(DbContextOptions options) : base(options) { @@ -1047,10 +1043,6 @@ namespace TestNamespace { public partial class TestDbContext : DbContext { - public TestDbContext() - { - } - public TestDbContext(DbContextOptions options) : base(options) { diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs index 77316438ecd..9f2452a7944 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs @@ -63,14 +63,15 @@ protected void Test( if (!skipBuild) { var assembly = build.BuildInMemory(); - var contextNamespace = options.ContextNamespace ?? options.ModelNamespace; - var context = (DbContext)assembly.CreateInstance( - !string.IsNullOrEmpty(contextNamespace) - ? contextNamespace + "." + options.ContextName - : options.ContextName); if (assertModel != null) { + var contextNamespace = options.ContextNamespace ?? options.ModelNamespace; + var context = (DbContext)assembly.CreateInstance( + !string.IsNullOrEmpty(contextNamespace) + ? contextNamespace + "." + options.ContextName + : options.ContextName); + var compiledModel = context.GetService().Model; assertModel(compiledModel); }