1

Looking for an explanation and education.

I created a class library for a code first database and using the app config created a connection string.

I then added that to a MVC project.

Referencing the class library db context in the MVC project I tried to create a simple record in one of the table and it generated an error.

However, when I put the copied the connection string into the web.config of the MVC project the record was created.

My question is, why do I need a connection string in the MVC project when I am using the db context from the class library which has it's own connection string? It seemed to me that data access would pass through the class library.

I found a similar question: Entity Framework 4.2 code first in a class library ignoring my connection string which seems to imply that as long as I have it in the class library I am ok.

Any insight you can provide on this would be appreciated. Also, let me know what code you need to see and I will provide it. Since I am unsure what you would need to look at initially.

Thank you in advance.

asked Feb 13, 2015 at 22:58

1 Answer 1

2

This is by design. All the configurations are read from the config file of the application that is acting as the host. This is because you could be using the same class library in others projects and, in your case, maybe connecting to different databases.

Please, look at this documentation Application Configuration Files :

The name and location of the application configuration file depend on the application's host

Nowadays, a web project with all the nuget packages and class libraries has a lot of dlls. It would be impossible to maintain if each of one has its own configuration file. I think it's better to be that way.

answered Feb 14, 2015 at 0:51
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.