Skip to content

Commands: Look for ConfigureServices in Program #3802

Closed

Description

@dneimke

I created a Console project and configured it to read the connection string during Startup and the app ran fine as expected. However, when I tried to use migrations using EF.Commands, I got an error saying that no database providers were configured.

To work around that I ended up creating a DbContextFactory which the Commands read and things worked fine.

https://github.com/dneimke/coffeestore/blob/develop/src/EF7Demo.CoffeeStore/Model/DbContextFactory.cs

I was surprised that I had to create the Factory as I do not need to do this for an ASP Web Application. Is this a different behavior for apps other than ASP.NET Web Application and was I right to create the Factory to work around this?

Activity

  1. rowanmiller commented on Nov 19, 2015

    @rowanmiller
    Contributor

    We have some special logic for ASP.NET 5 apps to run the code from startup and then resolve your context from DI.

    cc @bricelam who was looking at some improvements so that we could do the same outside of ASP.NET 5... not sure where we got to with those? Note there is a ConfigureServices in Program.

  2. dneimke commented on Nov 19, 2015

    @dneimke
    Author

    Thanks. Yeah I know that there is ConfigureServices in Program but I still couldn't get the EF Commands to work with just ConfigureServices in Program.

    It wasn't until I added the Factory that I could generate Migrations

  3. bricelam commented on Nov 19, 2015

    @bricelam
    Contributor

    The behavior is the same everywhere, but the ConfigureServices method still needs to be in a class called Startup. Looking inside Program (and App for XAML apps) came up during the design meeting, but we decided to stick with just Startup everywhere for now.

  4. dneimke commented on Nov 19, 2015

    @dneimke
    Author

    Ahh... got it. Thx

    On Fri, Nov 20, 2015 at 9:15 AM Brice Lambson [email protected]
    wrote:

    The behavior is the same, but the ConfigureServices method still needs to
    be in a class called Startup. Looking inside Program came up during the
    design meeting, but we decided to stick with just Startup everywhere for
    now.

    —
    Reply to this email directly or view it on GitHub
    #3802 (comment)
    .

  5. dneimke commented on Nov 20, 2015

    @dneimke
    Author

    I moved all of that code into a Startup.cs and now I no longer need the Factory. Thanks for the help!

    FYI, here's the updated code:

    https://github.com/dneimke/coffeestore/blob/develop/src/EF7Demo.CoffeeStore/Program.cs

  6. changed the title [-]Enabling Migrations in a Console App[/-] [+]Commands: Look for ConfigureServices in Program[/+] on Nov 20, 2015
  7. bricelam commented on Nov 20, 2015

    @bricelam
    Contributor

    I've updated the title of this bug to reflect your original expectations so we can discuss it as a team again.

  8. added this to the 7.0.0 milestone on Nov 20, 2015
  9. bricelam commented on Nov 20, 2015

    @bricelam
    Contributor

    We decided to search in Program and App in addition to Startup.

  10. dneimke commented on Nov 20, 2015

    @dneimke
    Author

    That's awesome, thanks guys!

  11. modified the milestones: , 7.0.0 on Dec 7, 2015
  12. modified the milestones: , 1.0.0 on Oct 15, 2022
  13. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions