You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Discussion for announcement: EF Core 2.0: design-time DbContext discovery changes #9033
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?
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?
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).
Referenced by IDesignTimeDbContextFactory implementations in the assembly or startup assembly
Registered in the application's service provider
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.
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)
@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.
@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?
@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
@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
@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.
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
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?
DbContext class public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) {} public DbSet<Location> Locations { get; set; } }
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.
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()?
@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!
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)
Activity
thiennn commentedon Jul 4, 2017
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?
divega commentedon Jul 4, 2017
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?
bricelam commentedon Jul 5, 2017
Nothing has changed. As part of discovering DbContext types, we have always tried to construct the application service provider.
IDesignTimeDbContextFactoryis only a hook for overriding the construction of DbContext types (not the discovery).ajcvickers commentedon Jul 5, 2017
@bricelam Do we need the service provider to discover context types?
bricelam commentedon Jul 5, 2017
We always look in three places:
IDesignTimeDbContextFactoryimplementations in the assembly or startup assemblyAfter 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.
bricelam commentedon Jul 5, 2017
We could also look into ways of short-circuiting the type discovery so
Startupisn't run if we find the specified type from its factory. (note, not specifying-Contextwould still need to look in the service provider to ensure there's still only one)ajcvickers commentedon Jul 5, 2017
@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.
thiennn commentedon Jul 5, 2017
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
ajcvickers commentedon Jul 5, 2017
@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?
thiennn commentedon Jul 5, 2017
@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
ajcvickers commentedon Jul 5, 2017
@thiennn Yes, but then what happens? Does the command complete successfully? Do you get an error?
thiennn commentedon Jul 5, 2017
@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
ajcvickers commentedon Jul 5, 2017
@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?
27 remaining items
Tratcher commentedon Sep 6, 2017
@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.
pcbjorklund commentedon Sep 13, 2017
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!
bricelam commentedon Sep 13, 2017
If you have a default constructor in your
DbContextor are using theProgram.BuildWebHost()pattern established in the ASP.NET Core 2.0 project templates, you typically won't need anIDesignTimeDbContextFactoryimplementation.pcbjorklund commentedon Sep 13, 2017
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()?
bricelam commentedon Sep 14, 2017
In 2.0.0-preview1 Program.BuildWebHost() wasn't used and you needed a design-time factory.
pcbjorklund commentedon Sep 14, 2017
Okay, thank you for the information @bricelam
DavidNorena commentedon Sep 29, 2017
Hi @bricelam, is there a way to resolve serivces configured inside a IDesignTimeDbContextFactory Implementation ?
bricelam commentedon Sep 29, 2017
Resolve them where? You may be able to use the
dbContext.GetService<T>()extension method.leopignataro commentedon Dec 13, 2017
@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 callingdb.Database.Migrate()(among many other things) onConfigure!bricelam commentedon Dec 13, 2017
There are improvements coming in version 2.1.
Startup.Configure()won't be called when things like EF are just trying to access theIServiceProvider. (see aspnet/Hosting#1263)memark commentedon Dec 14, 2017
@leopignataro @bricelam Indeed! Apart from this (rather obscure) thread I have never seen this mentioned before.
leopignataro commentedon Jan 29, 2018
Found an official recommendation:
https://docs.microsoft.com/en-us/aspnet/core/migration/1x-to-2x/#move-database-initialization-code