Skip to content

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

Closed

Description

@hahyes

Scaffold-DbContext is naming namespace wrong if -OutputDir is filled with phrase containing space.

For example: Example Folder OutputDir makes namespace ConsoleApp.Example Folder, what is wrong for naming namespaces and can't be builded. It should be ConsoleApp.Example_Folder.

Code

I made command through PMC like this:

Scaffold-DbContext "connectionString" MySql.EntityFrameworkCore -Tables some_tables_here -OutputDir "Example Folder" -Force

Command is working, but output classes and context have wrong namespace.

Include provider and version information

EF Core version: 5.0
Database provider: MySql.EntityFrameworkCore
Target framework: NET 5.0
Operating system:
IDE: Visual Studio 2019 16.9.3

Activity

  1. added this to the 6.0.0 milestone on Apr 9, 2021
  2. modified the milestones: 6.0.0, Backlog on Jul 30, 2021
  3. Psypher9 commented on Oct 16, 2021

    @Psypher9
    Contributor

    Is this open for contributions?

  4. Psypher9 commented on Oct 18, 2021

    @Psypher9
    Contributor

    @bricelam , so I've been looking at this a little bit and it looks like the private function that gets the subnamespace inside of the EFCore.Design.DatabaseOperations.ScaffoldContext needs to have a .Replace(' ', '_') added to this ternary before the string.Join('.',...) is evaluated.

    I also happened to notice that the GetNamespaceFromOutputPath method here and SubnamespaceFromOutputPath method here are the exact same inside of DbContextOperations.Optimize.

    I've been playing around with this in my own branch and I propose making a service to handle generating namespaces based on file paths and

    1. have both DbContextOperations.Optimize and DatabaseOperations.ScaffoldContext use that service
    2. have that service use the .Replace(' ', '_') to handle spaces in output paths.

    Another reason I propose making this a service is that, when I was looking to add tests there wasn't really a good way to make tests for this particular thing for either use case (at least to my knowledge). I've added tests to support my work so far, but is this something you guys really want and if so, do you really want to replaces spaces with _ or just remove them entirely?

    Thanks!

  5. bricelam commented on Oct 18, 2021

    @bricelam
    Contributor

    Hmm my initial thoughts on this were to use CSharpUtilities.GenerateCSharpIdentifier when generating the namespace in C#. This is because different languages have different requirements for what constitutes a valid identifier.

    But MSBuild also handles it similarly to the way you've proposed, so maybe it's ok...

    Happy to consider any efforts to avoid duplicate code.

  6. Psypher9 commented on Oct 18, 2021

    @Psypher9
    Contributor

    Ah okay, I'll check out the GenerateCSharpIdentifier method. I was unaware of that utility, so I'll how I can use it in this case. Hopefully, we won't need a new service!

    If MSBuild handles the namespace generation in the same way, I'll keep moving forward with that if you guys are alright with that. I'm happy to handle it anyway that fits with what you all want to do.

  7. bricelam commented on Oct 18, 2021

    @bricelam
    Contributor

    Yes, I'm OK with this approach. It would also be nice to do it for Migration class names.

  8. 6 remaining items

  9. modified the milestones: 7.0.0, Backlog on Jul 7, 2022
  10. removed their assignment
    on Jul 8, 2023
  11. added theissue type on Jun 15, 2025
  12. Empiree commented on Oct 4, 2026

    @Empiree
    Contributor

    Still happens on 10.0.12. dbcontext optimize has the same problem with --output-dir, the generated code doesnt compile there too.

    @roji I'd like to send a PR for both, already have it working locally. My idea is to replace bad chars with underscore like VS does, so "Example Folder" becomes Example_Folder. Is that ok or you prefer to just drop them like migrations do?

  13. AndriySvyryd commented on Oct 21, 2026

    @AndriySvyryd
    Member

    @Empiree Dropping the spaces would be preferable.

  14. Empiree commented on Oct 22, 2026

    @Empiree
    Contributor

    Thanks, makes sense. Opened #39386, it drops the invalid chars same as migrations do, so "Example Folder" gives ConsoleApp.ExampleFolder now. I fixed optimize there too since it had the same issue. The build is waiting for approval to run

  15. added a commit that references this issue on Oct 23, 2026
    5d22707
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions