Skip to content

Commit

Permalink
Scaffold-DbContext includes "using statement" with model´s namespace
Browse files Browse the repository at this point in the history
Summary of changes

- Add a new parameter to the WriteCode method of the ICSharpDbContextGenerator interface to pass the namespace of the models
- CSharpDbContextGenerator generates a using statement in the DbContext class if the models are in another namespace
- Add two test on CSharpDbContextGeneratorTest

Fixes #17839
  • Loading branch information
pacoweb authored and bricelam committed Oct 17, 2019
1 parent 75a5f2e commit 4d17b42
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/EFCore.Design/Scaffolding/Internal/CSharpDbContextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ public CSharpDbContextGenerator(
/// </summary>
public virtual string WriteCode(
IModel model,
string @namespace,
string contextName,
string connectionString,
string contextNamespace,
string modelNamespace,
bool useDataAnnotations,
bool suppressConnectionStringWarning)
{
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public interface ICSharpDbContextGenerator
/// </summary>
string WriteCode(
[NotNull] IModel model,
[NotNull] string @namespace,
[NotNull] string contextName,
[NotNull] string connectionString,
string contextNamespace,
string modelNamespace,
bool useDataAnnotations,
bool suppressConnectionStringWarning);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected void Test(
.BuildServiceProvider()
.GetRequiredService<IModelCodeGenerator>();

options.ModelNamespace = "TestNamespace";
options.ModelNamespace ??= "TestNamespace";
options.ContextName = "TestDbContext";
options.ConnectionString = "Initial Catalog=TestDatabase";

Expand Down

0 comments on commit 4d17b42

Please sign in to comment.