Chris
Chris

Reputation: 476

Entity Framework Code First Class Library

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.

Upvotes: 1

Views: 640

Answers (1)

Augusto Barreto
Augusto Barreto

Reputation: 3695

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.

Upvotes: 2

Related Questions