如何在asp.net核心中配置startup.cs以获取2个连接字符串?

时间:2017-05-30 15:17:28

标签: c# asp.net asp.net-core database-connection

我在appsettings.json上有两个连接字符串。一个包含用户的信息,第二个包含应用程序数据。我想问一下如何配置startup.cs以获取两个连接字符串

{   "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Warning"
    }   },   "Data": {
    "ConnectionStrings": {
      "RoomBookingDB": "Server=(localdb)\\MSSQLLocalDB;Database=RoomBookingDB;Trusted_Connection=True;",
      "Intranet": "Server=bdServer;Database=Intranet; ..."
    }   } }

1 个答案:

答案 0 :(得分:1)

我要做的是创建新类型来表示我的各种用例所需的设置。例如:

public class UserRepository(UserDatabaseSettings settings)
{
    //use settings.ConnectionString here
}

创建每个设置类的实例,并使用DI容器注册它们。

这将允许您的类要求正确类型的连接字符串...

        | TaskID | Hours1 | Hours2 | Hours3 | Hours4 | Hours5 | Hours6 | Hours7 |
        | 1      | NULL   | 10     | NULL   | NULL   | NULL   | NULL   | NULL   |
        | 2      | NULL   | NULL   | 0      | NULL   | NULL   | NULL   | NULL   |
        | 3      | 0      | NULL   | 0      | NULL   | 0      | 0      | NULL   |
        | 4      | 3      | 3      | 0      | 3      | NULL   | NULL   | 3      |
        | 5      | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   | NULL   |