-
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
Generate migration script without connection string #21055
Comments
This seems related to #19587. |
Likely a duplicate of #8427, which introduces parameterless UseSqlServer/UseSqlite without requiring a connection string. Note that as a workaround, you can specify some non-null non-empty connection string, which would not be consulted for generating the script. As long as you don't actually need a database connection (which you don't for script generation), this should work. |
Thanks @roji! A parameterless version of |
Right you are; I had missed that. Then I'm happy to close this issue, as it has been addressed. |
Duplicate of #8427 |
@roji Can now generate when
|
Remove migration checks if the migration has been applied to database or not. Hence it needs actual connectionstring. |
@smitpatel That works and I guess that makes sense, thanks |
@smitpatel - but update fails unless I put in a dummy connection string - even though I am assigning my connection string dynamically later. Currently I am doing that with a custom ISqlServerConnection implementation but that should be possible in a better way in 5 though I haven't explored that yet. |
Update applies migration to database, it needs actual connection string when you call Update. You can generate migration script and apply it directly to database outside of EF. |
@smitpatel Right. But it is getting the connection string dynamically - currently following approach described here - #19906 |
#8494 is fixed in 5.0 preview1. Are you using 5.0 preview version? Also, how are you dynamically replacing connection string? If your mechanism is also not in path of how design time services work then you may not be actually replacing connection string. Please file a new issue with details. |
@smitpatel Im ok with leaving the dummy connection string for now so not going to bother with another issue - but if your curious using 3.1 I am doing exactly what is described by the author in the first entry for issue at #19906 A later comment says "Something similar will be available in 5.0.0" but while I am now beginning to explore new 5.0 features, I have not yet attempted to change how Im dynamically setting the connection string. My first thought when switching to 5.0 was to simply get rid of the unused dummmy connection string passed to UseSqlServer as 5.0 makes that possible but apparently I cant do that. Perhaps if there really is a better approach to dynamically set a connection string when using a DbContextPool available in 5.0, it wont need the dummy connection string, I hope that is the case. |
This may be fixed for create a script, but it doesn't work for removing a migration... |
Hello @DuncanvR. I faced the same issue and did not get what was your solution to generating a migration script without a connection string. I did use parameterless but the script looks like that:
@smitpatel there are scenarios where you have no access to the production database but the migration script still needs to be generated. So to me, the question is still relevant. How to generate migration script without connection string? |
@MikeK93 I don't think you are facing this issue. You say you are using the parameterless overload of |
@DuncanvR, that is what I'm talking about. There's no easy way to generate a script with no actual connection to the real DB so that I could give that script to Ops guys. I guess EF could have some option to just generate that migration script and I don't think that would require much effort but the outcome of it would be huge. |
And we support it through parameterless overload of UseSqlServer. You posted the migration script generated but you are not telling anything about why you consider it to be wrong. The fact that it has generated script means that the script generation works fine. If the output is not as you expect then it is some other issue in your app config somewhere. Please file a new issue with detailed steps and repro code and what exactly you are expecting vs seeing. |
Context
We've got an ASP.NET Core app using EF Core (v3.1.4) to connect to an Azure SQL Server, which is being built and deployed in Azure DevOps. To update the database during releases, I'd like to:
dotnet ef migrations script --idempotent -o script.sql
in the build pipeline;script.sql
as a build artefact;Problem
The problem occurs in the first step, when generating the migration script in the pipeline, as an exception is thrown because the database connection string is not specified. We've set up the app to read the connection string from an environment variable, which isn't defined in the pipeline. I could add it there -- but I'd rather not, because it involves entering the database password -- while I don't understand why the tool needs it.
I can see it needs to know which database provider is used in order to create the correct SQL statements. But to actually generate the script, the information contained in the app (i.e. the migrations themselves) should be sufficient, right?
Attempts
I've tried specifying the following values as connection strings:
null
is passed as the connection string, this exception is thrown:";"
as the connection string, the tool happily generates the migration script.Suggestion
I realise the tool instantiates the database context using the code from the application. And for the application itself it can be useful to get a warning about the connection string being empty. But when generating the migration script, leaving it empty should be possible.
I'd therefore suggest changing
Microsoft.EntityFrameworkCore.Utilities.Check.NotEmpty()
to no longer throw an exception when it finds the given connection string is null or empty, but to print a warning instead.The text was updated successfully, but these errors were encountered: