From 08aa592a5b5d87e0a568211fa182895db9f1023a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Luthi?= Date: Wed, 27 Oct 2021 14:11:51 +0200 Subject: [PATCH] Do not scaffold a default constructor when OnConfiguring is suppressed Version 5.0.0 of the dotnet-ef tool introduced the `--no-onconfiguring` option which is great if you plan to configure your DbContext outside of the context itself (which you probably should). When using this option, the DbContext class is still generated with a default constructor. This default constructor becomes problematic without the `OnConfiguring` method since the context is unusable when created through the default constructor. As soon as you try to use a DbContext created by the default constructor, you get a `System.InvalidOperationException`: > No database provider has been configured for this DbContext. A provider can be configured by overriding the 'DbContext.OnConfiguring' method or by using 'AddDbContext' on the application service provider. If 'AddDbContext' is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext. Fixes #23515 --- .../Internal/CSharpDbContextGenerator.cs | 16 ++++++++++------ .../Internal/CSharpDbContextGeneratorTest.cs | 8 -------- .../Internal/ModelCodeGeneratorTestBase.cs | 11 ++++++----- 3 files changed, 16 insertions(+), 19 deletions(-) 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); }