-
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
Tools: Flow arguments into IDesignTimeDbContextFactory #8332
Comments
Another harebrained idea I had was to apply a convention to the unknown arguments. For example, |
@bricelam would something like this work for PowerShell?
Ideally I want to be able to copy and paste command line arguments I use for invoking the application into one argument of the PowerShell command and only make minimal edits. |
Yep. That would work too. |
(assuming you swapped the apostrophes and quotes) |
You'd also need to handle argument quoting and escaping yourself. Whereas with the array, we could handle that for you. |
Need this now :) Kind of relates directly to my question on SO: https://stackoverflow.com/questions/46583136/how-to-run-dotnet-ef-commands-to-target-a-specific-appsettings-environment-j#comment80159150_46583136 @bricelam , can I pass arguments and have this work with the current released version? |
No, this has not been implemented. You could read environment variables, a file, etc. inside IDesignTimeDbContextFactory. |
Note to implementer, we should allow them to be specified with |
Let me give you a use case that I think highlights why tackling this issue is important. When developing databases, I typically have a "development" database that my code-in-development uses. I also have a complementary "test" database that is used by my integration tests. This is separate from the "development" database so as not to interfere with that atomic nature of the integration tests (I run all tests in an ambient transaction, ensuring any database changes are rolled back after each test - that's another issue). In the past, I've used Fluent Migrations to keep all of my databases in sync (dev, test, staging, production) and was able to execute the database update by passing in an argument that specifies the connection string to pull from the app.config file in the migration project. Now that I'm trying to use EF Core 2.0 and code-first migrations, I can't find a way to specify the connection string to use when executing "dotnet ef database update". I created an implementation of the IDesignTimeDbContextFactory interface, but the args in the constructor are not used. Would be nice to be able to pass in the argument in the "update-database" command to indicate the connection string. |
For my use case i need to specify the connection string name to the command. Do we have an ETA on when it will be implemented? |
@ajcvickers i did it with an environment variable for now and it's working as expected, so i've created different batch file for all the jobs we need. but thanks for the reply, may be it's not documented enough on the document websites? |
@ajcvickers I need to be able to target a specific database on the command line. Reading from a file or environment variable won't work. Let's say I have a file that contains the connection strings for the dev and test databases. If I read from this file, how do I select the connection string I need? What I'd like to be able to do is something like this: C:> dotnet ef database update "test" Then in my implementation of IDesignTimeDbContextFactory, I can read a local config file and use the argument string passed in ("test", "dev", etc.) to select the correct connection string based on that key. I need something in IDesignTimeDbContextFactory that can affect the logic of the connection string selection. Hope that clarifies. I'm not sure what you're doing in your suggestion about environment variables. Does your batch job change the environment variable before the database update is executed? |
@dkehring Exactly, i do set ConnStringName=YourConnectionStringNameNeededForTheJob then i do dotnet ef database update or anything required. (in cmd) Here is the piece of code i use:
|
@ajcvickers Thanks for sharing. This is an interesting hack, but hardly an optimal solution. In my opinion, environment variables define the local machine environment and are - for the most part - fairly static. Sort of like the old machine.config file. Your solution works, but it's a work-around due to an incomplete implementation of IDesignTimeDbContextFactory. In fact, it's interesting that the input paramater "args" on IDesignTimeDbContextFactory.CreateDbContext is marked with the NotNull attribute. |
Decisions from Design Meeting 6/10/2020. Command-Line Powershell
For Powershell, the CmdLet just collects all the arguments and calls the command-line equivalent. Whatever is in the |
The approach above was checked in on June 16, 2020. For the purposes of the explanation below "normal arguments" are arguments or options that a particular command understands, which influence the way that command does its work, e.g. the name of the migration to create when calling "Application arguments", on the other hand, are any arguments which the command should pass on to whatever application is involved (usually one that creates and/or uses a Command-Line
Powershell The single
because the |
@lajones Have these changes been deployed yet? I've just updated to
For completeness sake, I am using a context factory and was hoping to swap out connection strings based on the command line args. |
@HugoPeters1024 I'm sorry but these changes were checked in to the 5.0 release rather than the 3.1 release, specifically they will be available in 5.0 preview 7. |
@lajones Oh that's my bad. Not sure how but I somehow failed to see that. I'll be looking forward to it then, cheers ;) |
@HugoPeters1024 No problem. When it is available let us know if there are any problems. Cheers. |
I can pass -AppArgs parameters with Add-Migration command. It Works. Nice. I also check it, with this -Migration parameter it works. But I can't pass with Update-Database command. I don't see all args parameters. |
@ismailkocacan I tried this with the preview 7 tools. The syntax to use is: Can you check that you have the preview 7 Tools package installed? |
@ajcvickers Yes, I checked. It works. Thanks. I installed dotnet ef 5.0.0-preview.7.20365.15. I execute below command and I don't see -args parameters ?
|
@ismailkocacan From the comment above, use this syntax:
|
@ajcvickers Did you read my ask ? I don't want to migrations add with dotnet ef. It works with Package Manager Console How can I specifiy args parameters with dotnet ef when I update database ? |
@ismailkocacan It's the same syntax:
|
EF Core cli doesn't seem to be working on version 5.0.0-preview.8.20407.4.
|
@macorx What version of the ef tool are you using? (Found by running |
|
@macorx I am not able to reproduce this. Please open a new issue and attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
@ajcvickers I found out what happened. I wrongly assumed I had to update only ef tool to version 5.0.0-preview.8.20407.4. After I have done that, it has worked as expected. |
We've enabled the ability for arguments to be passed into the Create method (via a
string[] args
parameter), but we need to think through the design of how they get specified on the command line.For
dotnet ef
andef
, I think it's obvious: any unknown argument will be passed verbatim.It gets a little more complicated on PowerShell where the paradigm is so different. Right now, the easiest way I can think of is to use a string array parameter.
The text was updated successfully, but these errors were encountered: