ErikEJ / EFCorePowerTools

Entity Framework Core Power Tools - reverse engineering, migrations and model visualization in Visual Studio & CLI
MIT License
2.07k stars 288 forks source link

Ability to use migrations tool without providing a connection string #2334

Closed blahDL closed 2 months ago

blahDL commented 2 months ago

I have a web application which dynamically sets the connection string used by the EF contexts based on request headers. When I start the Migrations Tool it spits out an application exception because the connection string has not been initialised (which is expected as the web application sets the connection string in the dependency injection registration using DbContextOptionsBuilder).

Is it possible to make the database connection string optional when using the migrations tool? Being able to specify/override a connection string in the options somewhere might work too if the database connection is required

image On a side note, the error message dialog seems to have a fixed width which makes it insanely long :-|

I currently type commands like these in the command prompt, it would be nice to have a GUI to to simplify these :-) dotnet ef migrations add --context=[DatabaseContext] [MigrationName] dotnet ef migrations script --context=[DatabaseContext] --idempotent --output script.sql

ErikEJ commented 2 months ago

Regarding the missing connection string - look at the advise here: https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli (I think you would encounter the exact same issues with the standard CLI tools=

dotnet ef migrations add --context=[DatabaseContext] [MigrationName]

dotnet ef migrations script --context=[DatabaseContext] --idempotent --output script.sql

ErikEJ commented 2 months ago

@blahDL Ping?

blahDL commented 2 months ago

Weird, I didn't get a notification email from the response or the ping :-|

We always generate the SQL files and run them against our databases, I don't think we have ever tried to run the migrations against a database via the CLI. We have a build step to generate the SQL and commit it to the repository to make it easier for everyone. I'll see what I can get working using that documentation though.

does the tool not do that for you already?

The connection string exception currently prevents me from getting to this functionality :-(

Please create a seperate issue for this, seems like a good idea.

Will do

blahDL commented 2 months ago

I was able to get the migration tool opening up now without throwing the exception by implementing IDesignTimeDbContextFactory for all of the database contexts from the documentation available at https://learn.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-a-design-time-factory

image

ErikEJ commented 2 months ago

Amazing!

ErikEJ commented 2 months ago

If you like my free tools, I would be very grateful for a rating or review on Visual Studio Marketplace or even a one-time or monthly sponsorship