10

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.

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

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

"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?

2 Answers 2

11

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

Which, when adapted to your example would become.

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

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.

Sign up to request clarification or add additional context in comments.

9 Comments

The second part doesn't work. I get an error on 'Configuration.GetConnectionString' stating: 'IConfigurationRoot' does not contain a definition for 'GetConnectionString' and no extension method 'GetConnectionString' accepting a first argument of type 'IConfigurationRoot' could be found (are you missing a using directive or an assembly reference?)
what version of asp.net are you using. check to make sure you are not omitting a using namespace
Neither does the first part it seems, I get a ArgumentNullException pointing at DefaultConnnection in the startupfile
how can I see which version I'm using? ASP.NET 5 MVC 6
when you created project, what template did you use
|
0

what you need for configuration in .net core 3.x is something like this or you have Iconfiguration injected in startup (This is for a command line project with 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");

and then you need to do this last bit for all connection strings you need to use.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.