-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Support file-scoped namespace declarations in generated .cs files #25223
Comments
Do we even care about older versions of C#? Typically, we've treated the default C# version of EF's minimum TFM as the minimum required version of C# in our tools. |
What happens when I use |
You'll get code generated by version 5.0 of the Microsoft.EntityFrameworkCore.Design package |
(Assuming you're using EF Core 5 since EF Core 6 requires .NET 6) |
Note also that if you're using EF Core 3.1 on .NET 6, you'll get code generated by EF Core 3.1. The version of dotnet-ef is largely irreleveant. |
Got it. That sounds great then. Are you suggesting then that perhaps migrations code should just be emitted using whatever language idioms we've decided are the most modern, and match what we've used in the project templates? |
Yep! See also #14545 |
I think another option is to do:
Of which I get bitten by both bullets, I use global usings and file scoped namespaces. |
@AraHaan adding a switch for each and every possible code generation option isn't a good approach. Note that for EF Core 7.0, we plan to add templating support, which would allow scaffolded models to be tweaked exactly as you want them. We'll likely make the default template modern (e.g. using file-scoped namespaces), but users would be able to tweak the template to remove one feature or another. |
@AraHaan And there is Handlebars template support available today. |
FYI there's early plans to have |
Our shiny new templates mechanism could somehow be aware of certain operation in .editorconfig and react accordingly? |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
@roji it would simply need to call |
Duplicate of #14545 |
This is a closed issue but EF Core is not respecting my project and generating the older style namespacing when generating a migration or is this going to be a .NET 7 thing. Or is there anything I should be doing to make it work aka a command line flag etc. Thanks all! |
@solrevdev This is covered by scaffolding templates in EF7. If you are using EF7, then you should get file-scoped namespaces by default, but can edit the templates to do something different. |
Ahh an EF7 thing. I will upgrade once its ready (I read November 2022) so for now I'll just fix manually no problem. Thanks @ajcvickers so much for replying! |
Hi @ajcvickers I think I may have misunderstood you because I've just upgraded my project to .NET 7 and EF Core 7 and When I create a migrations it causes a I dropped my database removed all migrations so that I was starting from creating a blank initial migration but that didn't help So I still have to manually go in and change the migration by hand by either converting to file scoped or for speed I just add an Am I missing something or doing something wrong? Do I need to edit a template or something? I did read the scaffolding templates article you suggested but I am not generating my model from an existing database I am going code first plus I didn't see anything about file scoped namespaces in that document. Any help would be appreciated as I've probably just ran out of talent! Thanks |
I feel like if you want to use file scoped namespaces that you must resort to doing database first. Besides I find using SSDT projects to be much easier to maintain than migrations (less files and less room for error and when there is a problem I can simply patch the sql file in the SSDT project then republish). |
@solrevdev EF7's templates covers database scaffolding (reverse engineering), not migration code. Modernizing migration C# code would be covered by #14545. @AraHaan I definitely don't think that missing file-scoped namespace declarations is a good reason to abandon migrations or switch to database first. |
Ahhh ok that makes sense! I’ll start watching the open issue #14545 Thanks for clearing that up for me much appreciated 🙏 |
C# 10 introduces support for file-scoped namespace declarations and the .NET 6 templates are being updated to use this new syntax .
It would be great if the .cs files generated into the project by the EF tooling (
dotnet ef
) supported emitting namespaces using this new syntax. It could either:The text was updated successfully, but these errors were encountered: