Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change connection string on command line to update database on migration to a new database in Entity Framework Core

In ASP.NET Core and EF Core Code First, I have a multi-tenant webapp. When I create a new tenant, I want to change connection string, add migration on it and then update.

From command line, add migration can't get connection string dinamically on tenant and so i haven't a valid connection string for create a new database in the first time the app starts with a new tenant. Is there any solution for it? Thanks

like image 724
Alessio Menichetti Avatar asked Aug 18 '16 08:08

Alessio Menichetti


People also ask

How do I change the database connection in Entity Framework?

Introduction. When using Entity Framework, the database connection string is stored in the app. config file by default, and the entity object references that to connect to the database. If, for any reason, a user needs to change the database, he can do so simply by editing that file prior to running the program.

How do I update my database in migration?

After creating a migration file using the add-migration command, you have to update the database. Execute the Update-Database command to create or modify a database schema. Use the –verbose option to view the SQL statements being applied to the target database.

How do I update my connection string?

In the Properties window, expand the Connection node. To quickly modify the connection string, edit the ConnectionString property, or click the down arrow on the Connection property and choose New Connection.

Which two main commands are used to add a new migration and update the database?

Creating or Updating the Database The Update command will create the database based on the context and domain classes and the migration snapshot, which is created using the add-migration or add command.


1 Answers

Are you looking for this? Edit: this is for EF 6.1

Update-Database –ConnectionString "Data Source=YourDatabaseServer; Initial Catalog=YourDatabase; Integrated Security=True;"
like image 197
Georg Patscheider Avatar answered Sep 18 '22 17:09

Georg Patscheider