-
Notifications
You must be signed in to change notification settings - Fork 3.3k
dotnet-ef migrations add
starts entire application when CreateHostBuilder method is not present
#28105
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
@marekott Running the application is an intentional change added by @davidfowl to support minimal APIs. @davidfowl @bricelam Any thoughts here? |
Related to #27306 (comment) |
That: #27322 (comment) would be best option for this case I believe. Since in docs you mentioned that:
Or allow to specify |
@davidfowl During application startup lot of things can be performed: microservice can send some message to the message bus indicating that it is started, it may register itself in Consul, some data seeding can take place. None of those is expected behavior when you are using EF tooling to generate new migration. Also application may require some mandatory configuration to be provided via env variables or Kubernetes mounted file (e.g. availability zone) without which it will throw, obviously you wouldn't want to provide this configuration just to generate EF migration. Long story short it is not expected behavior that adding database migration will start your application, but if you don't want to change existing behavior then at least add ability to directly specify in CLI |
I wonder if the Hosting code should have a switch (environment variable?) that avoids calling Main entirely. Seems like a good escape hatch to have... |
@bricelam I don't know what you have in mind when you say hosting code. If you mean that application should protect itself using some env variable from being unnecessarily started by EF tooling which just want to create migration then I think it is bad idea. BTW who should set this env variable? Whoever is executing migration before using |
@bricelam how does the |
@KrzysztofBranicki This code is not intended to be used by just EF tooling, but by any tooling that needs to obtain the service provider. Therefore, while #27322 (comment) and #27306 (comment) are things we intend to do on the EF side to make this better, these things won't help other commands that also use this mechanism, hence it is worth thinking about something more general in hosting that could be used by someone using any command. @davidfowl No, which is why #27322 (comment) is feasible. However, there is a chicken and egg problem in that if we don't know the registered context type (which might only be available via D.I.) then we might get the wrong factory type. #27322 (comment) recognizes that this is a corner case and that the value of assuming we have found the correct one to use is higher than the risk of getting it wrong in this corner case. |
@ajcvickers I understand that this code may be potentially used by some other tooling, assuming we would want to use any tooling that requires as to expose fully built web host with completely configured DI. My understanding was that whole purpose of I don't understand what you mean by chicken and egg problem. Currently |
@KrzysztofBranicki I agree that #27322 (comment) can be implemented, which is why the issue is open, not closed. I'm not sure what point you are trying to make. |
Closing since EF work here is covered by other issues. |
Faced the same issue with the app start up, but it was a little bit tricky. I use Autofac and have one service registered as Changing |
In which tasks exactly the problem will be solved? |
TLTR
When
public static IHostBuilder CreateHostBuilder(string[] args)
method is removed fromProgram.cs
in a web api template project with dependencies on EF 6, generating migration with dotnet ef CLI tool starts the entire application instead of generating migration. In EF 5 this behaviour was not present.Hello,
I've encountered strange behaviour of the dotnet ef CLI tool after updating my application to EF 6.
Program.cs
in our microservices looks like this:As you can see it lacks
CreateHostBuilder(string[] args)
because it wouldn't be productive to add this method in all microservices. Instead, we provide a helper method as above. Also, what is important, our application is implementingIDesignTimeDbContextFactory
in order to control creating ofDbContext
when migrations are being added (find simple reproduction here: https://github.com/marekott/dotnet-ef-add-migration-bug-reproduction). When application had dependency on EF 5dotnet-ef migrations add Initial -v
command produced valid migration (logs below). After updating to EF 6 the same command starts the entire application (logs below) that needs to be killed in order to finish producing migration (which in the end is a valid one). AddingCreateHostBuilder(string[] args)
resolves this issue, application is not started and migration is generated. However, adding this method especially that it is not used and wasn't needed for 3 years seem as a lazy solution.Taking everything into consideration what is a reason for this strange behaviour? It seems like a bug, especially that it has worked previously.
Logs for EF 5
Logs for EF 6
My dotnet ef version is:
The text was updated successfully, but these errors were encountered: