-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating different database via CLI (Pass connection string to dotnet ef database update
)
#10750
Comments
Notes from triage: this is something we are considering for the future. It may depend on #6525 before it can be implemented. For now, the way to do this is to pull the connection string from configuration and either change the configuration for each run or use something like environment variables. |
dotnet ef database update
)
Could you please elaborate on the idea of using environment variables a little bit please? How whould you use that? I have one Model (DbContext) and 10 databases created with this context. In runtime I create the Context with constructor that takes the database name as parameter and puts that into connection string in OnConfiguring method. Now I would like to create migration and update all 10 databases. My idea was to create cli migration script but fot that I would need the migration commant to accept connection string parameter (this got me to this issue). I am not sure how I can use environment variables for that... |
@urza The idea is to change something in a script that can be read inside your code. So, for example, set an environment variable that contains the connection string, then run database updates in a loop changing the value of the environment variable each time. |
Is there a way right now to have the startup-project configure the connection string so that when the I don't fully understand what's the need of the startup project other than targeting the .net framework. In fact:
Then I would expect the migration creation to fail. But it succeeds. So the PS: To better understand my question. I just closed this: #19228 |
If all the
that should be available somewhere in the PATH and then have something such as
Later on, whether some env variable or appsettings is used to source this parameters for the CLI, that shouldn't be a problem imo. |
I have an EF Core 3.0 project that contains EF entities and DataContext. It's part of a larger framework which includes DI. The connection string will be handled through that app framework and supplied in a normal fashion. However, to even create an initial migration in the EF project there has to be a connection string. If I can't pass it as a command line arg to dotnet ef migrations add then I have to hard-code it inside the data context - that makes no sense. How did we get to this point with EF where we absolutely must have a program.cs with an appconfig json with a connection string inside it just to set up and maintain EF in a separate library? I understand the need to talk to a database obviously but I should be able to supply the connection string on the command line itself, not require all kinds of Core plumbing in disparate projects within the same solution just to get the add migration command to work. |
The solution for me here was to temporarily update the Startup.cs file within the project which depends on EF Core and manually set the connection string to the connstring specific to the env, then run I then just repeated the process of updating the connection string for the next env, and reran dotnet ef database update |
Similiarly, I needed this feature to faciliate running migrations as a step in my DevOps Pipeline, but retrieveing the connection details from a keyvault, rather than leaving them in a file in source control so I ended up leveraging the
This way you can set the connection string as a standard argument to the Note: This will also run with every argument style for like Hope this helps in the mean time |
In my case I have a multi-tenancy app with database-per-tenant approach. How can I run migrations since connection strings are retrieved dynamically by host? (Now I'm hardcoding a connection string just for migrations). Maybe it could be great to create migrations without the need of a connection string in order to create the code. Later, an update-database with a new optional field, let's say --connection-string. This opens the possibility to create a script and apply a specific migration to the connection strings I want. |
I ended up with and in startup using |
A Workaround that works good for me: protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connectionString = "-";
try // raises an exception when input is piped
{
if (!Console.KeyAvailable)
connectionString = "-";
}
catch
{
// blocks when input is not piped
if (Console.In.Peek() != -1)
connectionString = Console.ReadLine();
}
optionsBuilder.UseOracle(connectionString);
}
} With CLI:
|
The EF Core CLI, does not allow me to switch databases via command line. It would be great, to either specify the appsettings via cli to pull the data from, or to specify the connection string via cli parameter.
The text was updated successfully, but these errors were encountered: