You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Scaffold-DbContext can name namespace wrong if phrase with space is used on OutputDir #24607
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.
@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'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
have both DbContextOperations.Optimize and DatabaseOperations.ScaffoldContext use that service
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?
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.
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.
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?
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
Activity
Psypher9 commentedon Oct 16, 2021
Is this open for contributions?
Psypher9 commentedon Oct 18, 2021
@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.ScaffoldContextneeds to have a.Replace(' ', '_')added to this ternary before thestring.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
DbContextOperations.OptimizeandDatabaseOperations.ScaffoldContextuse that service.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!
bricelam commentedon Oct 18, 2021
Hmm my initial thoughts on this were to use
CSharpUtilities.GenerateCSharpIdentifierwhen 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.
Psypher9 commentedon Oct 18, 2021
Ah okay, I'll check out the
GenerateCSharpIdentifiermethod. 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.
bricelam commentedon Oct 18, 2021
Yes, I'm OK with this approach. It would also be nice to do it for Migration class names.
6 remaining items
Empiree commentedon Oct 4, 2026
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?
AndriySvyryd commentedon Oct 21, 2026
@Empiree Dropping the spaces would be preferable.
Empiree commentedon Oct 22, 2026
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
Drop invalid characters from namespaces built from output folder names (