使用多个连接字符串 [英] Using multiple connection strings

查看:133
本文介绍了使用多个连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Info
I have multiple projects in my Solution, of which one is the DAL and the other is an ASP.NET MVC6 project. Since the MVC6 project is also the startup project I need to add my connection string there.

我看到了此解决方案,但是它不被接受,也不起作用.

I saw this solution, but it is not accepted, nor does it work.

我的尝试
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连接字符串访问数据时,出现以下消息:

Yet, when I try to access data using the FooBar connection string I get the following message:

"其他信息:不能将名为"FooBar"的连接字符串作为 在应用程序配置文件中找到."

"Additional information: No connection string named 'FooBar' could be found in the application config file."

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

The Question
How do I get multiple connection strings working?

推荐答案

如果您查看

If you take a look at the official documentation for connection strings in asp.net core their example shows the connection string stored in appsettings.json like this

{
  "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()方法和配置键

Configuring the context in Startup.cs with the configuration string being read from configuration would use the GetConnectionString() method with the configuration key

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")));
}

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

Now one observed issue with how the above context is configured in the original question is that there are now two connection strings for the same context.

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

Trying to use multiple connection strings to work for the same context will cause problems as the framework would not know which option to use when requesting the context.

这篇关于使用多个连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆