user1108948
user1108948

Reputation:

The connection string is empty in Web Application

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

Upvotes: 0

Views: 718

Answers (2)

Scott Hannen
Scott Hannen

Reputation: 29207

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.

Upvotes: 0

Fran
Fran

Reputation: 6520

put the connection string in the web.config.

Upvotes: 1

Related Questions