简体   繁体   English

如何在命令行上更改连接字符串以在迁移到Entity Framework Core中的新数据库时更新数据库

[英]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. 在ASP.NET Core和EF Core Code First中,我有一个多租户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 谢谢

Are you looking for this? 你在找这个吗? Edit : this is for EF 6.1 编辑 :这适用于EF 6.1

Update-Database –ConnectionString "Data Source=YourDatabaseServer; Initial Catalog=YourDatabase; Integrated Security=True;"

I solved by inserting inside the DbContext constructor: 我通过在DbContext构造函数中插入来解决:

Database.EnsureCreated();

Thanks for your reply. 感谢您的回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM