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
Reverse engineering: Add IsConfigured check to avoid using hard-coded connection when previously configured #6686
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?
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?
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 ConfigurationManager in OnConfiguring, or if it's an ASP.NET core app then the code is probably already there in Startup.cs. Perhaps we need to take another look at whether we can realistically work out what kind of app we are generating into.
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 DbContextOptions<TContext> and no OnConfuguring() method is a better way to go about this.
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.
@ElanHasson suppressing OnConfiguring only gets you part of the way there, as you need to also add the constructor that takes options. We discussed adding this flag (implemented as you did) but decided it was just a stop gap that we would want to remove once we have proper support for doing something better with connection strings (i.e. something that tailors to the specific app model you are using).
@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?
In my scenario, we're injecting DbContextOptions into the ctor in a different partial class, and having a hard-coded connection string in the OnConfiguring method is actually preventing the injected DbContextOptions<> from being used due to configuration priority.
Perhaps we can modify the generated method to include if (!optionsBuilder.IsConfigured)... then use the hardcoded connection string.
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.
changed the title [-]Hard-coded connection string in DB Context after using scaffolding tool[/-][+]Reverse engineering: Add IsConfigured check to avoid using hard-coded connection when previously configured[/+]on May 9, 2017
Activity
divega commentedon Oct 5, 2016
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?
cbrianball commentedon Oct 5, 2016
Yes, that would be great!
rowanmiller commentedon Oct 5, 2016
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
ConfigurationManagerinOnConfiguring, or if it's an ASP.NET core app then the code is probably already there in Startup.cs. Perhaps we need to take another look at whether we can realistically work out what kind of app we are generating into.cbrianball commentedon Oct 5, 2016
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.
rowanmiller commentedon Oct 5, 2016
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).
divega commentedon Oct 5, 2016
@rowanmiller Fair point. I think having an option to generate the constructor that takes external
DbContextOptions<TContext>and noOnConfuguring()method is a better way to go about this.rowanmiller commentedon Oct 10, 2016
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.
ElanHasson commentedon Oct 13, 2016
@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?
ElanHasson commentedon Oct 13, 2016
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.
rowanmiller commentedon Oct 13, 2016
@ElanHasson suppressing
OnConfiguringonly gets you part of the way there, as you need to also add the constructor that takes options. We discussed adding this flag (implemented as you did) but decided it was just a stop gap that we would want to remove once we have proper support for doing something better with connection strings (i.e. something that tailors to the specific app model you are using).ElanHasson commentedon Nov 2, 2016
@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?
In my scenario, we're injecting DbContextOptions into the ctor in a different partial class, and having a hard-coded connection string in the OnConfiguring method is actually preventing the injected DbContextOptions<> from being used due to configuration priority.
ElanHasson commentedon Nov 2, 2016
Perhaps we can modify the generated method to include
if (!optionsBuilder.IsConfigured)...then use the hardcoded connection string.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.
rowanmiller commentedon Nov 2, 2016
Interesting idea, re-opening to discuss in triage
ElanHasson commentedon Nov 17, 2016
Great to see this moving along...
ElanHasson commentedon Nov 19, 2016
@rowanmiller and @smitpatel thanks for taking care of this so quickly.
Glad I was able come up with a simple, fast solution!!
[-]Hard-coded connection string in DB Context after using scaffolding tool[/-][+]Reverse engineering: Add IsConfigured check to avoid using hard-coded connection when previously configured[/+]