简体   繁体   English

Package 管理器控制台如何知道要使用哪个连接字符串?

[英]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-migrationupdate-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-migrationupdate-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-migrationupdate-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.jsonappsettings.Development.jsonappsettings.Production.json ,每个都有不同的连接字符串。

The one used was appsettings.Development.json .使用的是appsettings.Development.json

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

相关问题 使用包管理器控制台“ Update-Database”等在实体框架中以编程方式连接字符串 - Programmatically connection string in entity framework with package manager console “Update-Database” etc 启用迁移在 Package 管理器控制台中不起作用 - Enable Migrations does not work in Package Manager Console NServiceBus如何知道要使用哪个DLL? - How does NServiceBus know which DLL to use? Console.WriteLine()如何知道如何将对象转换为字符串 - How does Console.WriteLine() know how to convert an object to a string 如何使用正确的连接字符串 .net 核心控制台应用程序 - How to use proper connection string .net core console application WPF设计器如何知道要使用哪个资源? - How does the WPF Designer know which resource to use? 该模型工厂如何知道使用哪种方法? - How does this model factory know which method to use? 资源服务器如何知道使用哪种算法来验证 JWT? - How does the resource server know which algorithm to use to verify a JWT? EF Core使用NUGET Package Manager控制台或CMD - EF Core to use NUGET Package Manager Console or CMD 卡在 asp.net 核心教程中。 尝试使用 package 管理器控制台添加 EF Core SQL 服务器提供程序不起作用 - Stuck in asp.net core tutorial. Trying to use package manager console to add EF Core SQL Server provider does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM