-
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
Reverse engineering: Add IsConfigured check to avoid using hard-coded connection when previously configured #6686
Comments
I think we could add an option to skip generating the connection string which you could use if you already have it in the right place. Would that help? |
Yes, that would be great! |
We would have to generate code to wire up the context to use the existing connection string though, so depending on the app the code we'd have to generate could be |
I don't know the details around the experience you are looking to provide, but as a developer, I think it's perfectly reasonable for it to be my responsibility to worry about how the connection string is set on the DbContext. |
My main point was that if we skipped generating the connection string then you wouldn't have to delete it from code... but you would still be in the position where you have to hand edit the context file to wire back up the connection string (or add the DI-friendly constructor if you are using ASP.NET Core). |
@rowanmiller Fair point. I think having an option to generate the constructor that takes external |
We decided we aren't going to do anything right now, but opened a more general item to look at better support for the different connection string patterns #6746. We think this may be intertwined with our reverse engineer template story, possibly with some built-in templates you can easily chose between. |
@rowanmiller am I to understand this discussion as "the connection string will be hard coded for now" ? Why not add a flag to disable it as @divega suggested? |
Being unfamiliar with the EF Core codebase, I could have probably implemented this better-- but it seems pretty straight forward... ElanHasson@2d2c0dd I didn't yet implement any tests, and have not submitted a PR for it. Based on the above discussion, would it even be accepted? If so, I'll add tests. |
@ElanHasson suppressing |
@rowanmiller sorry, didn't get an alert for your response. Scaffolding is a major part of the DB first workflow especially with regard to automation.. currently the only other options I can see are using Roslyn/String manipulation to remove the method and this is not ideal. Regarding the "something better", is that even slated/planned/identified? I'd be happy to take a stab at it if it isn't. Perhaps the low-effort, half-measure is what I've implemented in ElanHasson@2d2c0dd . It is already generating all the classes as partials, so perhaps it's an exercise left to the implementor? |
Perhaps we can modify the generated method to include This should be a great low-effort modification that can satisfy the DI use case without introducing additional overhead, such as documentation, and the creation of many net-new unit tests, etc. |
Interesting idea, re-opening to discuss in triage |
Great to see this moving along... |
@rowanmiller and @smitpatel thanks for taking care of this so quickly. Glad I was able come up with a simple, fast solution!! |
I may be misunderstanding the intent of the tool: once I run
dotnet ef dbcontext scaffold ...
, all of my files are generated as expected, but the provided connection string is hard-coded in the dbcontext class itself (with a warning directing the developer to follow best-practices on storing a connection string).I had planned to do my database schema updates externally (more specifically from a SQL Server database project), so I will need to run this tool whenever there are any schema changes, which means, each time it runs, I'll have to manually modify the generated database context (or script the change).
Is there an option/pattern I'm missing, or I am not following the intended usage of the tool?
The text was updated successfully, but these errors were encountered: