Just starting playing with the .Net Core RC2 by migrating a current MVC .Net app I developed. It looks like to me because of the way that configuration is handled with appsettings.json that if I have multiple connection strings I either have to use EF to retrieve a connectionstring or I have to create separate classes named for each connection string. All the examples I see either use EF (which doesn't make sense for me since I will be using Dapper) or the example builds a class named after the section in the config. Am I missing a better solution?
"Data": {
"Server1": {
"ConnectionString": "data source={server1};initial catalog=master;integrated security=True;"
},
"Server2": {
"ConnectionString": "data source={server2};initial catalog=master;integrated security=True;"
}
}
Why would I want to build two classes, one named "Server1" and another "Server2" if the only property each had was a connectionstring?
I don't like the idea of instantiating the DAL. Rather, I'd do something like this
And something like this in the ctor of the DAL
Now you have all the connection strings in the DAL object. You can use them on each query or even select it by index on every call.
There are a couple of corrections that I made to Adem's response to work with RC2, so I figured I better post them.
I configured the appsettings.json and created a class like Adem's
and
most of Adem's code comes out of the box in VS for RC2, so I just added the line below to the ConfigureServices method
The main missing point is that the connection string has to be passed to the controller (Once you’ve specified a strongly-typed configuration object and added it to the services collection, you can request it from any Controller or Action method by requesting an instance of IOptions, https://docs.asp.net/en/latest/mvc/controllers/dependency-injection.html)
So this goes to the controller,
and then when you instantiate the DAL you pass the appropriate connectionString
All the examples show this, they just don't state it, why my attempts to pull directly from the DAL didn't work
You can use
Options
to access inDAL
layer. I will try to write simple example(RC1):First you need to create appsettings.json file with below content:
Then create a class:
And in
Startup.cs
Finally inject it in the
DAL
class: