【问题标题】:Using multiple connection strings使用多个连接字符串
【发布时间】:2017-05-22 09:44:33
【问题描述】:

信息
我的解决方案中有多个项目,其中一个是 DAL,另一个是 ASP.NET MVC6 项目。 由于 MVC6 项目也是启动项目,我需要在此处添加我的连接字符串。

我看到this solution,但是不接受,也不行。

我的尝试
appsettings.json

"Data": {
  "DefaultConnection": {
    "ConnectionString": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "FooBar": {
    "ConnectionString": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddEntityFramework()
        .AddSqlServer()
        .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]))
             .AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration["Data:FooBar:ConnectionString"]));
}

然而,当我尝试使用FooBar 连接字符串访问数据时,我收到以下消息:

"附加信息:没有名为 'FooBar' 的连接字符串 在应用程序配置文件中找到。”

问题
如何让多个连接字符串正常工作?

【问题讨论】:

    标签: c# json asp.net-core asp.net-core-mvc connection-string


    【解决方案1】:

    如果您查看 asp.net 核心中的 official documentation for connection strings,他们的示例显示了存储在 appsettings.json 中的连接字符串,如下所示

    {
      "ConnectionStrings": {
        "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
      },
    }
    

    当适应你的例子时会变成这样。

    {
      "ConnectionStrings": {
        "DefaultConnection": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true",
        "FooBar": "Server=.\\SQLEXPRESS;Database=Bar;Trusted_Connection=True;MultipleActiveResultSets=true"
      }
    }
    

    使用从配置中读取的配置字符串配置Startup.cs 中的上下文将使用带有配置键的GetConnectionString() 方法

    public void ConfigureServices(IServiceCollection services) {
        // Add framework services.
        services
            .AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnextionString("DefaultConnection")))
            .AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnextionString("FooBar")));
    }
    

    在原始问题中如何配置上述上下文的一个观察到的问题是,现在有两个连接字符串用于同一上下文。

    尝试使用多个连接字符串为同一个上下文工作会导致问题,因为框架在请求上下文时不知道使用哪个选项。

    【讨论】:

    • 第二部分不起作用。我在“Configuration.GetConnectionString”上收到一条错误消息:“IConfigurationRoot”不包含“GetConnectionString”的定义,并且找不到接受“IConfigurationRoot”类型的第一个参数的扩展方法“GetConnectionString”(您是否缺少 using 指令还是程序集参考?)
    • 您使用的是什么版本的 asp.net。检查以确保您没有省略 using 命名空间
    • 第一部分似乎也没有,我得到一个 ArgumentNullException 指向启动文件中的 DefaultConnnection
    • 如何查看我使用的版本? ASP.NET 5 MVC 6
    • 你创建项目的时候用的什么模板
    【解决方案2】:

    .net core 3.x 中的配置需要是这样的 或者您在启动时注入了 Iconfiguration(这是用于带有 args 的命令行项目)。

            IConfiguration Configuration = new ConfigurationBuilder()
           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
           .AddEnvironmentVariables()
           .AddCommandLine(args)
           .Build();
    
            string conString = Microsoft
                   .Extensions
                   .Configuration
                   .ConfigurationExtensions
                   .GetConnectionString(Configuration, "ConnectionName");
    

    然后你需要为你需要使用的所有连接字符串做这最后一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2021-02-03
      • 1970-01-01
      • 2012-02-02
      • 2014-06-02
      • 1970-01-01
      相关资源
      最近更新 更多