Home:ALL Converter>Command line connection string for EF core database update

Command line connection string for EF core database update

Ask Time:2017-04-14T01:35:57         Author:NP3

Json Formatter

Using ASP.NET Core and EF Core, I am trying to apply migrations to the database. However, the login in the connection string in appsettings.json that the app will use has only CRUD access, because of security concerns, so it can't create tables and columns, etc. So, when I run:

dotnet ef database update -c MyDbContextName -e Development

I want to tell it to use a different connection string, but I don't know if this can be done? Basically, I want to use two different connection strings, one for deployment and one for running the app. Is this possible? Is there a better approach? Thanks.

Author:NP3,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/43398483/command-line-connection-string-for-ef-core-database-update
lets do it :

In EF Core 5.0, you will pass the connection string in the command line like this,\n\ndotnet ef database update --connection \"connection string\"\n\n\nReference: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#new-command-line-parameters-for-namespaces-and-connection-strings",
2020-06-12T08:06:08
Ilya Chumakov :

Keep both connection strings in appsettings.json. Inherit a child context class from the main one and override OnConfiguring with another connection string:\n\npublic class ScaffoldContext : MyDbContextName \n{\n protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)\n {\n string scaffoldConnStr = ConfigurationManager.ConnectionStrings[\"scaffoldConnStr\"].ConnectionString;\n\n optionsBuilder.UseSqlServer(scaffoldConnStr);\n }\n}\n\n\nThen use:\n\ndotnet ef database update -c ScaffoldContext \n",
2017-04-13T18:58:03
superjos :

I liked the idea of a scaffolding DbContext, but I found some issues (somehow solvable, I guess) and also some consideration on where to keep connection strings. Those led me to another, more crude solution, so I thought I'd share all that here. \n\nThis is about the general approach, and consequent solution:\n\n\nI don't like to store staging/production connection strings under source control. So I would pass that through an environment variable set in a temporary command window (or maybe on the fly on command-line, although I was not able to make that work). At that point, if I'm setting an environment variable, I might as well override initial connection string used by MyDbContextName, instead of adding a new one. Thus the whole scaffolding DB context thing could be overcome doing this way.\n\n\nOther issues I found along the way:\n\n\nInitial DbContext had dependencies injected into constructor, so child context had to to the same. This made dotnet ef commands complain about missing parameterless constructor.\nTo overcome that, child context as well was registered at startup with .AddDbContext<ChildDbContext>(...). This also required for ChildDbContext to be injected both with a DbContextOptions<ParentDbContext> as well as a DbContextOptions<ChildDbContext>. After that, dotnet-ef still found issues with instantiating ChildDbContext, as that needed also a dependency on IConfiguration which could not be found. Maybe (?) this is due to the fact that dotnet-ef does not run through the whole application startup.\n\n\nAs I said, I guess the issues could be solved after all, but still I'm questioning the real value of a scaffolding context in case you don't want to save connection strings in dedicated appsettings files. One counter argument could be that you might forget the environment variable set to the remote connection string, but then you just have to close the command window as soon as you complete migration. HTH",
2017-09-11T19:55:47
Javier Colombera :

In a Linux terminal:\nexport ASPNETCORE_ENVIRONMENT="Development"\ndotnet ef database update\n\nWhere ASPNETCORE_ENVIRONMENT match with appsettings.Development.json and the properly connectionstring configured.",
2020-11-12T15:44:50
Mahmoud Farahat :

if you are using the Package Manager Console use the following command:\nUpdate-Database -Connection "YOUR_CONNECTION_STRING"",
2021-02-07T12:09:50
KGC :

I solved it this way, inspired by Javier Colombera's answer.\nI ran the following on Windows in a Bash terminal and got the expected result:\nexport ASPNETCORE_ENVIRONMENT="Production"\nexport ConnectionStrings__DefaultConnection="SslMode=Prefer;TrustServerCertificate=true;Host=<value>;Port=<value>;Database=<value>;Username=<value>;Password=<value>"\ndotnet ef database update\n\nThis lets me avoid storing my production database connection string in version control, by overwriting the ConnectionStrings.DefaultConnection value in appsettings.json via an environment variable.\nNote that my project does not define a default connection string for the Staging and Production environments, only one is defined for the local Development environment.",
2021-04-24T18:31:16
yy