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

2

8

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

Phagy answered 18/8, 2016 at 8:11 Comment(0)
C
3

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

Update-Database –ConnectionString "Data Source=YourDatabaseServer; Initial Catalog=YourDatabase; Integrated Security=True;"
Contiguous answered 18/8, 2016 at 9:3 Comment(0)
P
-1

I solved by inserting inside the DbContext constructor:

Database.EnsureCreated();

Thanks for your reply.

Phagy answered 18/8, 2016 at 9:39 Comment(2)
That's not a good solution. Every time an instance of your DbContext is created it will perform this check. You want to perform migrations once on startup. See my answer here https://mcmap.net/q/1473071/-run-database-migrations-using-entity-framework-core-on-application-startSaltation
Thank you, your solution is better than mine. The important is to create at least one migration for each context.Phagy

© 2022 - 2024 — McMap. All rights reserved.