Skip to content

Drop invalid characters from namespaces built from output folder names - #39386

Merged
AndriySvyryd merged 1 commit into
dotnet:mainfrom
Empiree:fix/scaffold-namespace-output-dir
Oct 22, 2026
Merged

AndriySvyryd merged 1 commit into
dotnet:mainfrom
Empiree:fix/scaffold-namespace-output-dir

Conversation

@Empiree

@Empiree Empiree commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Fixes #24607

Scaffold and dbcontext optimize used the output folder names as is when building the namespace. So a folder like "Example Folder" or "My-Models" gave a namespace that doesnt compile. For optimize only the using in the assembly attributes file was broken, the namespace itself was already cleaned there.

Now the folder names go through the same helper migrations use, so you get ConsoleApp.ExampleFolder like discussed in the issue. Migrations are not changed.

Added tests in DatabaseOperationsTest and DbContextOperationsTest. I ran the new tests locally but not the whole suite, so lets see what CI says.

Scaffold and dbcontext optimize used the folder names as is, so a folder with a space or a dash gave a namespace that doesnt compile. Now they go through the same helper as migrations.

Fixes dotnet#24607

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

The scaffolding regression test does not verify the sanitized namespace emitted in generated entity files.

No open issues 🎉

What changed in this PR

This pull request sanitizes namespaces derived from scaffold and compiled-model output folder names so generated C# remains valid.

Changes:

  • Normalize output-directory namespace segments through ICSharpHelper.Namespace.
  • Add regression coverage for spaces, punctuation, numeric names, nested paths, and separate context directories.
  • Preserve explicit namespaces and existing migration behavior.
File Reviewed changes
test/​EFCore.Design.Tests/​Design/​Internal/​DbContextOperationsTest.cs Adds coverage for valid namespaces in optimized compiled-model output.
test/​EFCore.Design.Tests/​Design/​Internal/​DatabaseOperationsTest.cs Adds scaffolding namespace regression coverage, but does not verify the sanitized namespace in generated entity files.
src/​EFCore.Design/​Design/​Internal/​DbContextOperations.cs Sanitizes namespaces derived from compiled-model output directories.
src/​EFCore.Design/​Design/​Internal/​DatabaseOperations.cs Sanitizes namespaces derived from reverse-engineering output directories.

Review effort: Lite

@AndriySvyryd

Copy link
Copy Markdown
Member

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) October 22, 2026 23:22
@AndriySvyryd
AndriySvyryd merged commit 5d22707 into dotnet:main Oct 22, 2026
19 of 20 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scaffold-DbContext can name namespace wrong if phrase with space is used on OutputDir

3 participants