Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scaffold-DbContext includes "using statement" with model´s namespace #18405

Merged
merged 1 commit into from
Oct 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should keep old API for provider compatibility.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless this goes to master.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, is ICSharpDbContextGenerator .Internal too? I feel like it should be

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too late to make anything internal for a while now...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's Internal. This isn't breaking.

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, ";"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd use...

_sb.Append("using ").Append(modelNamespace).AppendLine(";");

}

_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