Closed dpparekh closed 4 years ago
This is a very interesting question! The short answer is that you don’t need a connecting string because when the migration scripts are generated there is no need to connect to the database. dotnet ef
is used to generate the scripts, and if you run this on a command line you will see that there is no parameter for connection string:
dotnet ef migrations add –help
But… you need a connection string anyway! As I understand it the tool is launching your application in some special way. I guess you have something like this in your application:
services.AddDbContext<CatDatabaseContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("CatDatabaseContext")));
When the tool is running, it will try to instantiate CatDatabaseContext
and when this happens it will try to read connection string from your settings. And in your case I guess you do not have any connection string in your application settings.
One way to solve this is to add something in your application settings. As I said, no connection will be done to any database so it could be any connection string as long as the format is valid.
But there is a more interesting way to solve this. By default, settings are not only read from appsettings.json, but also from environment variables.
So, my guess is that if you run this in PowerShell before you run the script generate, it might work:
$env:ConnectionStrings__CatDatabaseContext="Server=localhost;Database=SomeDbName;Trusted_Connection=True"
Another option might be that you add ConnectionStrings__CatDatabaseContext
as a variable to your pipeline.
I guess one of these works, but I do not know which one. But I would love you to try :-)
Thank for your reply. I did exactly what you said and everything worked. I ended up creating a pipeline variable.
Is there a way to pass the connectionString to the task? I am getting the value from KeyVault in a previous task in AzureDevOps and want to pass it to the script generator.