[英]How does Package Manager Console know which connection string to use?
I have a website application.我有一个网站应用程序。 Depending on the
IsDemoSite
setting in my appsettings.json file, the application uses one of two different database connection strings.根据我的appsettings.json文件中的
IsDemoSite
设置,应用程序使用两个不同的数据库连接字符串之一。 (One for my main site, and another for our demo site.) (一个用于我的主站点,另一个用于我们的演示站点。)
I also have a class library that contains all the Entity Framework models and migrations.我还有一个 class 库,其中包含所有实体框架模型和迁移。
In the Package Manager Console, I set the Default project to the class library.在 Package 管理器控制台中,我将默认项目设置为 class 库。
Then, I can run the add-migration
and update-database
commands on that class library.然后,我可以在该 class 库上运行
add-migration
和update-database
命令。 But here's the kicker: The IsDemoSite
setting for my main application determines which database these commands work with.但更重要的是:我的主应用程序的
IsDemoSite
设置决定了这些命令使用哪个数据库。
How on Earth does Package Manager Console know to what connection string is used by my main application based on the current setting? Package 管理器控制台究竟如何根据当前设置知道我的主应用程序使用的连接字符串? I'm not running the main application.
我没有运行主应用程序。 Package Manager Console is not using the main application as the default project.
Package 管理器控制台未将主应用程序用作默认项目。 How the heck does it know which connection string to use?
它到底是怎么知道要使用哪个连接字符串的?
When you run and add-migration
or an update-database
command, Package Manager Console will look for the Startup project of your solution and use it as "entry point" for these commands.当您运行并
add-migration
或update-database
命令时,Package 管理器控制台将查找您的解决方案的启动项目并将其用作这些命令的“入口点”。
Probably inside your Startup.cs
class you have the database context injected using the connection string that is stored in your appsettings.json.可能在您的
Startup.cs
class 中,您使用存储在 appsettings.json 中的连接字符串注入了数据库上下文。
I believe you have something like that:我相信你有这样的事情:
string connectionString;
bool isDemoSite = configuration.GetValue<string>("IsDemoSite");
if(isDemoSite)
connectionString = configuration.GetConnectionString("mainSite");
else
connectionString = configuration.GetConnectionString("demoSite");
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));
add-migration
and update-database
uses this very same code to determinate against which database it will execute, is something like your application is beign executed to allow this commands to run. add-migration
和update-database
使用相同的代码来确定它将执行哪个数据库,就像您的应用程序被执行以允许此命令运行一样。
That's also the reason you need some EntityFramework packages installed in your startup project.这也是您需要在启动项目中安装一些 EntityFramework 包的原因。
I was wondering which appsettings.json would the application use if i used Package Manager Console from Visual Studio 2022. I had appsettings.json
, appsettings.Development.json
and appsettings.Production.json
, each with different connection string.我想知道如果我使用 Visual Studio 2022 中的 Package 管理器控制台,应用程序会使用哪个 appsettings.json 。我有
appsettings.json
、 appsettings.Development.json
和appsettings.Production.json
,每个都有不同的连接字符串。
The one used was appsettings.Development.json
.使用的是
appsettings.Development.json
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.