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

Scaffold tool: add a flag to generate the constructor with DbContextOptions injected #11570

Closed
ryanelian opened this issue Apr 6, 2018 · 2 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

@ryanelian
Copy link

Currently, scaffold existing database tool will generate an OnConfiguring method with connection string inside:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    #warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
    optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");
}

I realized this result is by-design to make the the result of reverse engineering to be runnable (#10432), however, shouldn't there be an flag for people who needs an injected DbContextOptions instead?

--inject-dbcontextoptions

public BloggingContext(DbContextOptions<BloggingContext> options)
    : base(options)
{ }

With this flag, we can re-scaffold the context from existing database without touching the constructor again.

I mean, people who use EF Core in ASP.NET Core MVC app will tend to add the DB context into the service collection using AddDbContextPool...

@ryanelian ryanelian changed the title Scaffold tool: add a flag to generate constructor with DbContextOption Scaffold tool: add a flag to generate the constructor with DbContextOptions injected Apr 6, 2018
@ajcvickers
Copy link
Member

Discussed in triage and adding to the backlog. It seems like it may be fine to generate both constructors. For now, the constructor can be added to a partial class.

@ajcvickers ajcvickers added this to the Backlog milestone Apr 9, 2018
smitpatel added a commit that referenced this issue Apr 9, 2018
smitpatel added a commit that referenced this issue Apr 10, 2018
smitpatel added a commit that referenced this issue Apr 10, 2018
@smitpatel smitpatel self-assigned this Apr 10, 2018
@smitpatel smitpatel modified the milestones: Backlog, 2.1.0 Apr 10, 2018
@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 Apr 10, 2018
@Jrubzjeknf
Copy link

For anyone wondering how to use this functionality: you must specify your connectionstring to be named, which looks like name=MyConnectionString. The name of your connection string corresponds with the one defined in your appsettings.json.

@ajcvickers ajcvickers modified the milestones: 2.1.0-rc1, 2.1.0 Nov 11, 2019
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

4 participants