Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line connection string for EF core database update

Tags:

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.

like image 625
NP3 Avatar asked Apr 13 '17 17:04

NP3


People also ask

How do I update EF core tools?

Update the tools Use dotnet tool update --global dotnet-ef to update the global tools to the latest available version. If you have the tools installed locally in your project use dotnet tool update dotnet-ef . Install a specific version by appending --version <VERSION> to your command.

What is better way to update data in EF core?

Update Data With Entry() Method In EF core Because you're creating a new instance (which isn't tracked) instead of updating the existing instance (which is tracked).

How do I update my Entity Framework database first?

Right-click anywhere on the design surface, and select Update Model from Database. In the Update Wizard, select the Refresh tab and then select Tables > dbo > Student. Click Finish.


2 Answers

In EF Core 5.0, you will pass the connection string in the command line like this,

dotnet ef database update --connection "connection string" 

Reference: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-5.0/whatsnew#new-command-line-parameters-for-namespaces-and-connection-strings

like image 118
vivek nuna Avatar answered Sep 17 '22 12:09

vivek nuna


Keep both connection strings in appsettings.json. Inherit a child context class from the main one and override OnConfiguring with another connection string:

public class ScaffoldContext : MyDbContextName  {     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)     {         string scaffoldConnStr = ConfigurationManager.ConnectionStrings["scaffoldConnStr"].ConnectionString;          optionsBuilder.UseSqlServer(scaffoldConnStr);     } } 

Then use:

dotnet ef database update -c ScaffoldContext  
like image 27
Ilya Chumakov Avatar answered Sep 18 '22 12:09

Ilya Chumakov