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

Design: Discover DbContext types using Program.BuildWebHost() #8164

Closed
bricelam opened this issue Apr 13, 2017 · 17 comments
Closed

Design: Discover DbContext types using Program.BuildWebHost() #8164

bricelam opened this issue Apr 13, 2017 · 17 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

@bricelam
Copy link
Contributor

If ASP.NET Core adds a well known, static accessor to the IWebHost, our tools can use this to instantiate the DbContext at runtime.

We may also want to reconsider some aspects of the previous Startup-based mechanism which leaked a bit into our codebase. Specificly: environment and content root directory. This includes the --environment option on dotnet ef and ef.exe, the environment and contentRootPath parameters on OperationExecutor, and the ContentRootPath (see also #8159) and EnvironmentName properties on `DbContextFactoryOptions. Issue #7480 is also probably superseded by this.

We may also want to flow arbitrary command-line arguments into the static entry point and IDbContextFactory. But I'm not sure how we'll handle the transition from PowerShell to command-line...

This work depends on #7588

@bricelam
Copy link
Contributor Author

As part of revisiting DbContextFactoryOptions, we can remove ContentRootPath (this was tracked by #8159)

@bricelam
Copy link
Contributor Author

#7353 may also be related to this work.

@bricelam bricelam modified the milestones: 2.0.0-preview1, 2.0.0 Apr 24, 2017
@bricelam
Copy link
Contributor Author

We currently allow a ConfigureDesignTimeServices method to be defined on the startup class. We should consider removing that convention and only allow implementations of IDesignTimeServices.

@bricelam
Copy link
Contributor Author

bricelam commented Apr 24, 2017

Questions we need to answer:

  • Can we remove --environment and DbContextFactoryOptions.EnvironmentName? This would make Environment a Hosting-only concern.
  • Can we remove DbContextFactoryOptions.ContentRootPath? This would make ContentRootPath a Hosting-only concern.
  • Can we remove the logic that attempts to call <startup>.Configure<environment>Services directly? (about 600 lines of code)
  • Do we try and call <startup>.ConfigureDesignTimeServices when using Program.BuildWebHost?
  • How do we flow arguments into BuildWebHost from dotnet-ef/ef? From PMC?

@bricelam bricelam removed the tools label Apr 26, 2017
@bricelam
Copy link
Contributor Author

Decisions:

  • Remove --environment
  • Remove DbContextFactoryOptions
  • Require IDesignTimeServices to be implemented before calling ConfigureDesignTimeServices
  • For now, arguments will be empty at design-time. Add IDbContextFactory.Create(string[] args) to enable flowing in the future.

Still need to discuss this:

  • Can we remove the logic that attempts to call <startup>.Configure<environment>Services directly? (about 600 lines of code)

@bricelam
Copy link
Contributor Author

We will no longer attempt to call Startup.ConfigureServices directly. Use Program.BuildWebHost or IDbContextFactory instead.

@bricelam
Copy link
Contributor Author

After #8314, #8326 & dotnet/templating#705 are merged, the 2.0.0-preview1 work for this item is done. We'll gather feedback and see if we need to do anything more in this area.

I'll file a new issue to reflect the work that was done for 2.0.0-preview1 and move this one into 2.0.0 for to follow up.

@bricelam
Copy link
Contributor Author

Filed #8328

@bricelam bricelam added this to the 2.0.0 milestone Apr 28, 2017
@bricelam bricelam removed this from the 2.0.0-preview1 milestone Apr 28, 2017
@bricelam bricelam changed the title Tools: Use static IWebHost accessor Design: Discover DbContext types using Program.BuildWebHost() Apr 28, 2017
@bricelam
Copy link
Contributor Author

Inlining @ajcvickers's comment.

Think we need a better message that indicates that this has happened not because they did something "wrong" but because we have made a change in how things work in 2.0. Probably with a fwlink to docs on how to convert. But happy for all this to happen later.

@bricelam
Copy link
Contributor Author

bricelam commented May 16, 2017

This has evolved a bit since originally filing. The current plan is to use the interface described in aspnet/DependencyInjection#524 and not try to call Program.BuildWebHost directly.

@bricelam bricelam changed the title Design: Discover DbContext types using Program.BuildWebHost() Design: Discover DbContext types using IDesignTimeServiceProviderFactory May 16, 2017
@bricelam
Copy link
Contributor Author

With #8510, we'll now use Program.BuildWebHost if it exists.

@bricelam bricelam changed the title Design: Discover DbContext types using IDesignTimeServiceProviderFactory Design: Discover DbContext types using Program.BuildWebHost() May 18, 2017
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 18, 2017
@shawnwildermuth
Copy link

Is there any good way to know if we're in 'design-time' since you're running the entire Startup path? I can't seem to find how to know if dotnet ef is running us instead of dotnet run. Any ideas>

@bricelam
Copy link
Contributor Author

bricelam commented Jul 3, 2017

Implementing IDesignTimeDbContextFactory is the easiest way.

@shawnwildermuth
Copy link

So if I have both, it will use IDesignTimeDbContextFactory first if found?

@ajcvickers
Copy link
Contributor

@shawnwildermuth Not currently, but see #9076, #9033, and #9073.

@bricelam
Copy link
Contributor Author

bricelam commented Jul 6, 2017

@shawnwildermuth Yes. IDesignTimeDbContext factory will be used to construct the DbContext if it's present.

However, per the issues mentioned above, we may still (despite implementing the factory) call Program.BuildWebHost to probe for additional DbContext types in the application service provider. The workaround to avoid this is to make the BuildWebHost method private rename it.

@shawnwildermuth
Copy link

I talked about my approach (since all the examples I've seen is use the same method to build up the webhost) in my blog. It feel hacky (and I mention it as such). Feel free to burn me a little for this monstrosity. https://wildermuth.com/2017/07/06/Program-cs-in-ASP-NET-Core-2-0

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview2, 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

3 participants