diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs index 9386c8cf6c3..13a1f49dbe4 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs @@ -62,9 +62,10 @@ public CSharpDbContextGenerator( /// public virtual string WriteCode( IModel model, - string @namespace, string contextName, string connectionString, + string contextNamespace, + string modelNamespace, bool useDataAnnotations, bool suppressConnectionStringWarning) { @@ -75,9 +76,17 @@ public virtual string WriteCode( _sb.AppendLine("using System;"); // Guid default values require new Guid() which requires this using _sb.AppendLine("using Microsoft.EntityFrameworkCore;"); _sb.AppendLine("using Microsoft.EntityFrameworkCore.Metadata;"); + + var finalContextNamespace = contextNamespace ?? modelNamespace; + + if (finalContextNamespace != modelNamespace) + { + _sb.AppendLine(String.Concat("using ", modelNamespace, ";")); + } + _sb.AppendLine(); - _sb.AppendLine($"namespace {@namespace}"); + _sb.AppendLine($"namespace {finalContextNamespace}"); _sb.AppendLine("{"); using (_sb.Indent()) diff --git a/src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs index 8217a6bda05..7b3c038789b 100644 --- a/src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/CSharpModelGenerator.cs @@ -78,9 +78,10 @@ public override ScaffoldedModel GenerateModel( var generatedCode = CSharpDbContextGenerator.WriteCode( model, - options.ContextNamespace ?? options.ModelNamespace, options.ContextName, options.ConnectionString, + options.ContextNamespace, + options.ModelNamespace, options.UseDataAnnotations, options.SuppressConnectionStringWarning); diff --git a/src/EFCore.Design/Scaffolding/Internal/ICSharpDbContextGenerator.cs b/src/EFCore.Design/Scaffolding/Internal/ICSharpDbContextGenerator.cs index 916b049f782..44dd735ede7 100644 --- a/src/EFCore.Design/Scaffolding/Internal/ICSharpDbContextGenerator.cs +++ b/src/EFCore.Design/Scaffolding/Internal/ICSharpDbContextGenerator.cs @@ -22,9 +22,10 @@ public interface ICSharpDbContextGenerator /// string WriteCode( [NotNull] IModel model, - [NotNull] string @namespace, [NotNull] string contextName, [NotNull] string connectionString, + string contextNamespace, + string modelNamespace, bool useDataAnnotations, bool suppressConnectionStringWarning); } diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs index 674491e5c4a..31dc3807ef3 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/CSharpDbContextGeneratorTest.cs @@ -194,6 +194,41 @@ public void Views_work() model.FindEntityType("TestNamespace.Vista").FindAnnotation(RelationalAnnotationNames.ViewDefinition))); } + [ConditionalFact] + public void ModelInDiferentNamespaceDbContext_works() + { + var modelGenerationOptions = new ModelCodeGenerationOptions() + { + ContextNamespace = "TestNamespace", + ModelNamespace = "AnotherNamespaceOfModel" + }; + + const string entityInAnoterNamespaceTypeName = "EntityInAnotherNamespace"; + + Test(modelBuilder => modelBuilder.Entity(entityInAnoterNamespaceTypeName) + , modelGenerationOptions + , code => Assert.Contains(string.Concat("using ", modelGenerationOptions.ModelNamespace, ";"), code.ContextFile.Code) + , model => Assert.NotNull(model.FindEntityType(string.Concat(modelGenerationOptions.ModelNamespace, ".", entityInAnoterNamespaceTypeName))) + ); + } + + [ConditionalFact] + public void ModelSameNamespaceDbContext_works() + { + var modelGenerationOptions = new ModelCodeGenerationOptions() + { + ContextNamespace = "TestNamespace", + }; + + const string entityInAnoterNamespaceTypeName = "EntityInAnotherNamespace"; + + Test(modelBuilder => modelBuilder.Entity(entityInAnoterNamespaceTypeName) + , modelGenerationOptions + , code => Assert.DoesNotContain(string.Concat("using ", modelGenerationOptions.ModelNamespace, ";"), code.ContextFile.Code) + , model => Assert.NotNull(model.FindEntityType(string.Concat(modelGenerationOptions.ModelNamespace, ".", entityInAnoterNamespaceTypeName))) + ); + } + private class TestCodeGeneratorPlugin : ProviderCodeGeneratorPlugin { public override MethodCallCodeFragment GenerateProviderOptions() diff --git a/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs b/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs index af0ded01ea2..b0a87de75c3 100644 --- a/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs +++ b/test/EFCore.Design.Tests/Scaffolding/Internal/ModelCodeGeneratorTestBase.cs @@ -36,7 +36,7 @@ protected void Test( .BuildServiceProvider() .GetRequiredService(); - options.ModelNamespace = "TestNamespace"; + options.ModelNamespace ??= "TestNamespace"; options.ContextName = "TestDbContext"; options.ConnectionString = "Initial Catalog=TestDatabase";