0

I have a asp.net mvc web application to display a table in the view.

Also I have a data access project (it is a class library, the dll of this project will be put into the above MVC project and accessing data in that).

My web application doesn't do the data access things. The class library takes it. It contains the connection string in app.config and entity framework data context is also in the library. Now the library works well in console application. But it fails in the web application because the connection string is empty.

Basically in the class library, the connection string can be obtained:

 public class EndpointManagement : IEndpointManagement
 {
    public string GetConnectionString(MyDataContext context)
    {
       return ConfigurationManager.AppSettings["connectionString"];
    }
 }

Not sure how to deal with it in web application????

2 Answers 2

1

put the connection string in the web.config.

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

1 Comment

why did this get down voted? He wrote a console app with an app.config. He wants to use the library that needs the configuration appSetting in an mvc application. If he puts the connectionString in the appSettings section of the web.config, it will flow through to the library.
0

I see this come up a lot - a class library requiring an <appSetting> or connection string from the host application's web.config or app.config.

It's problematic because the dependency on the connection string is "hidden" inside the class library. Someone references the library, it fails with a null reference exception, and the only way to find the cause is to look through the library's source code. There it is - it's expecting a value in <appSettings>.

It's best to avoid a direct dependency on app.config anywhere in our classes. But if it can't be helped, I recommend at least throwing a very clear exception - "Class Xyz requires a connection string..." and specify where it's looking for that connection string. At least that way the consumer doesn't have to guess and read extra source code.

Comments

Your Answer

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