Skip to content
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

Closed
cbrianball opened this issue Oct 5, 2016 · 15 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@cbrianball
Copy link

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?

@divega
Copy link
Contributor

divega commented 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
Copy link
Author

Yes, that would be great!

@rowanmiller
Copy link
Contributor

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.

@cbrianball
Copy link
Author

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
Copy link
Contributor

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
Copy link
Contributor

divega commented Oct 5, 2016

@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.

@rowanmiller
Copy link
Contributor

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
Copy link

@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
Copy link

ElanHasson commented 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
Copy link
Contributor

@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).

@ElanHasson
Copy link

ElanHasson commented 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
Copy link

ElanHasson commented 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
Copy link
Contributor

Perhaps we can modify the generated method to include if (!optionsBuilder.IsConfigured)... then use the hardcoded connection string.

Interesting idea, re-opening to discuss in triage

@rowanmiller rowanmiller reopened this Nov 2, 2016
@divega divega added this to the 1.2.0 milestone Nov 4, 2016
@ElanHasson
Copy link

ElanHasson commented Nov 17, 2016

Great to see this moving along...

@smitpatel smitpatel assigned smitpatel and unassigned lajones Nov 18, 2016
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Nov 19, 2016
@ElanHasson
Copy link

@rowanmiller and @smitpatel thanks for taking care of this so quickly.

Glad I was able come up with a simple, fast solution!!

@ajcvickers ajcvickers 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 May 9, 2017
@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

7 participants