0

I'm trying to port a Web Application from ASP.NET 4.5 to ASP.NET 5 / MVC 6 (Beta 8).

The application consists of a WebApp and a class library which encapsulates the DB access. In my new solution, the class library is of type Class Library (Package).

The DB access layer (class library) tries to find a connection string named MyConnection. In my old ASP.NET 4.5 application I used to define that connection string in web.config of the WebApp. Somehow this configuration found it's way to my class library so that my class library was able to access the connection string defined in the WebApp.

Now with ASP.NET 5 I analogously tried to define the connection string in appsettings.json like this:

{
  "ConnectionStrings": {
    "MyConnection": "server=localhost\\SQLEXPRESS;database=MyDb;Integrated Security=True"
  }
}

But at runtime I get the following error:

Can't find a connection string with the name 'MyConnection'

So, unlike with old ASP.NET, here the configuration doesn't seem to make its way down to the referenced class library.

How can I make sure that my connection string is visible from my referenced class library?

5
  • 1
    you need to create a class for your connection string setting and in startup you bind the class from the appsettings.json and you need to inject it into your class library similar to this question about user secrets whihc is another place you could put your connection string. There is no more static access to config settings. stackoverflow.com/questions/32599573/… Commented Nov 11, 2015 at 20:01
  • Thanks for your input. But with this approach it won't be possible to access that data using ConfigurationManager.ConnectionStrings[...], right? The thing is that I'm using a third-party library which tries to access the connection string based on its name that way... Commented Nov 11, 2015 at 20:14
  • Ok that third-party library also has a constructor which accepts a ConnectionString and a ProviderName, instead of a ConnectionStringName. Now I'm passing these two values using the mechanism described in your link. That helped - thank you very much! Commented Nov 11, 2015 at 20:31
  • @JoeAudette: Please post your comment as an answer so the OP can accept it. Commented Nov 11, 2015 at 21:07
  • done, glad I could be off help Commented Nov 11, 2015 at 21:13

1 Answer 1

1

you need to create a class for your connection string setting and in startup you bind the class from the appsettings.json and you need to inject it into your class library similar to this question about user secrets which is another place you could put your connection string. There is no more static access to config settings.

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

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.