Skip to content

Discussion for announcement: EF Core 2.0: design-time DbContext discovery changes #9033

Closed as not planned

Description

@divega

This is the discussion thread for aspnet/Announcements#258.

Activity

  1. added this to the Discussions milestone on Jun 30, 2017
  2. thiennn commented on Jul 4, 2017

    @thiennn

    In 2.0 Preview 1. I implemented IDbContextFactory because I don't want Add-Migration, Update-Database,... to run my Startup.cs. It worked as expected.

    In Preview 2, I changed to IDesignTimeDbContextFactory, but when I run Add-Migration, I see it run both ConfigureServices and Configure in Startup.cs before CreateDbContext of IDesignTimeDbContextFactory. Is this a expected new behavior?

  3. divega commented on Jul 4, 2017

    @divega
    ContributorAuthor

    In Preview 2, I changed to IDesignTimeDbContextFactory, but when I run Add-Migration, I see it run both ConfigureServices and Configure in Startup.cs before CreateDbContext of IDesignTimeDbContextFactory. Is this a expected new behavior?

    I suspect we changed the order in which things are discovered. I can't recall if we made an explicit decision that this was ok. @bricelam?

  4. bricelam commented on Jul 5, 2017

    @bricelam
    Contributor

    Nothing has changed. As part of discovering DbContext types, we have always tried to construct the application service provider. IDesignTimeDbContextFactory is only a hook for overriding the construction of DbContext types (not the discovery).

  5. ajcvickers commented on Jul 5, 2017

    @ajcvickers
    Contributor

    @bricelam Do we need the service provider to discover context types?

  6. bricelam commented on Jul 5, 2017

    @bricelam
    Contributor

    We always look in three places:

    1. Referenced by IDesignTimeDbContextFactory implementations in the assembly or startup assembly
    2. Registered in the application's service provider
    3. DbContext derived types in the assembly or startup assembly

    After we've discovered them all, we pick one to use.

    I've considered having a way to bypass the type discovery (e.g. specify an assembly-qualified name on the command) and then discover a construction mechanism (factory first, service provider second, default constructor last), but this would require a completely separate (new) code path, and the value always seemed minimal.

  7. bricelam commented on Jul 5, 2017

    @bricelam
    Contributor

    We could also look into ways of short-circuiting the type discovery so Startup isn't run if we find the specified type from its factory. (note, not specifying -Context would still need to look in the service provider to ensure there's still only one)

  8. ajcvickers commented on Jul 5, 2017

    @ajcvickers
    Contributor

    @bricelam I think it's very useful to have an escape hatch that doesn't involve trying to build the application's service provider since doing so could have side effects that need to be avoided. I had assumed that having a factory was the way to do that. Maybe the short circuiting would be the way to enable this.

    Moving temporarily out of Discussions so we can triage this.

  9. removed this from the Discussions milestone on Jul 5, 2017
  10. thiennn commented on Jul 5, 2017

    @thiennn

    I hadn't deep dived into the internal implementation of the type discovery. But I have code for both 2.0 preview 1 work as expected https://github.com/simplcommerce/SimplCommerce/tree/master and 2.0 preview 2 not work as expected https://github.com/simplcommerce/SimplCommerce/tree/netcore2preview2

  11. ajcvickers commented on Jul 5, 2017

    @ajcvickers
    Contributor

    @thiennn When you say, "not work as expected" can you be more specific? Does it throw an exception? If so, what is the exception message and stack trace? If it doesn't throw an exception, then other than it running code when you don't expect it to, what is not working?

  12. thiennn commented on Jul 5, 2017

    @thiennn

    @ajcvickers like my earlier comment. In 2.0 preview1: Add-Migration, Update-Database command doesn't invoke Startup.cs. But in 2.0 preview 2, it run both ConfigureServices and Configure of Startup.cs

  13. ajcvickers commented on Jul 5, 2017

    @ajcvickers
    Contributor

    @thiennn Yes, but then what happens? Does the command complete successfully? Do you get an error?

  14. thiennn commented on Jul 5, 2017

    @thiennn

    @ajcvickers In 2.0 preview 2, the command run into my Startup.cs which have some dependencies that not ready at design time. If I remove these dependencies, the command complete successfully.

    What I expected is the command should not invoke my Startup.cs because I already implemented IDesignTimeDbContextFactory

  15. ajcvickers commented on Jul 5, 2017

    @ajcvickers
    Contributor

    @thiennn If you don't remove those dependencies, what happens? Do you get an error? If so, what's the error message/output/stack trace?

  16. 27 remaining items

  17. Tratcher commented on Sep 6, 2017

    @Tratcher
    Member

    @memark EF doesn't 'want' to run Startup.Configure(), it's just a side effect of the new BuildWebHost pattern. It runs all of Startup before it returns the services that EF needs.

  18. pcbjorklund commented on Sep 13, 2017

    @pcbjorklund

    Hi,
    I've got a question about the IDesignTimeDbContextFactory.
    Previously i had to implement IDesignTimeDbContextFactory to get migrations running, e.g: PM > Add-Migration Initial PM > Update-Database

    If not, the console threw an error and led me here: https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext#use-idesigntimedbcontextfactory.

    So i did what it suggested and got migrations running. After that i have created new projects, and i didn't have to implement the IDesignTimeDbContextFactory. Migrations worked anyway. How is this possible? Same .NET Core version (2.0) on all of the projects.

    Do we always need to create a IDesignTimeDbContextFactory if we want to use migrations from the Package Manager Console, or is it just in certain situations?

    The below did't work at first, but once implementing the IDesignTimeDbContextFactory in one project, all the following new projects worked without it. Coincidence or did something in the background got updated?

    Startups ConfigureServices:
    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer("the_connection_string"));

    DbContext class
    public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {} public DbSet<Location> Locations { get; set; } }

    Thanks!

  19. bricelam commented on Sep 13, 2017

    @bricelam
    Contributor

    If you have a default constructor in your DbContext or are using the Program.BuildWebHost() pattern established in the ASP.NET Core 2.0 project templates, you typically won't need an IDesignTimeDbContextFactory implementation.

  20. pcbjorklund commented on Sep 13, 2017

    @pcbjorklund

    Thanks for the response @bricelam
    I'm using the Program.BuildWebHost() (as it shipped with the project template for ASP.NET Core 2.0) and my constructor in my DbContext class is passing the DbContextOptions to the base (DbContext).
    It all works well now, i'm just a bit confused why it all of a sudden started to work woithout the IDesignTimeDbContextFactory for new projects. When you say "typically", are there rare occasions when the IDesignTimeDbContextFactory could be needed even though running 2.0 and the BuildWebHost()?

  21. bricelam commented on Sep 14, 2017

    @bricelam
    Contributor

    In 2.0.0-preview1 Program.BuildWebHost() wasn't used and you needed a design-time factory.

  22. pcbjorklund commented on Sep 14, 2017

    @pcbjorklund

    Okay, thank you for the information @bricelam

  23. DavidNorena commented on Sep 29, 2017

    @DavidNorena

    Hi @bricelam, is there a way to resolve serivces configured inside a IDesignTimeDbContextFactory Implementation ?

  24. bricelam commented on Sep 29, 2017

    @bricelam
    Contributor

    Resolve them where? You may be able to use the dbContext.GetService<T>() extension method.

  25. leopignataro commented on Dec 13, 2017

    @leopignataro

    @bricelam , this "Application startup code should go inside Program.Main()" recommendation is a huge deal. If this is indeed coming from Microsoft, this should definitely be clearly stated on MS docs. Think of how many projects there are out there calling db.Database.Migrate() (among many other things) on Configure!

  26. bricelam commented on Dec 13, 2017

    @bricelam
    Contributor

    There are improvements coming in version 2.1. Startup.Configure() won't be called when things like EF are just trying to access the IServiceProvider. (see aspnet/Hosting#1263)

  27. memark commented on Dec 14, 2017

    @memark

    @leopignataro @bricelam Indeed! Apart from this (rather obscure) thread I have never seen this mentioned before.

  28. leopignataro commented on Jan 29, 2018

    @leopignataro
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions