How to define the EF connection string only once and re-use it in other projects?
Question
I am building a web application that makes use of Entity Framework. I have moved the Entity Model and generated classes to a separate project, because it will be used by more than one consumer.
But when I try to run the application, Entity Framework tells me that
No connection string named 'X' could be ofund in the application config file.
To remove this problem, I would have to add the Entity Framework connection string to every consuming project. Of course this is annoying, because there are several consumers, and also it introduces a tight coupling that i hoped to get rid by dividing the software into different projects.
So is it possible to define the connection string in only one place and not in every consuming project?
NOTE: I am following the Database First approach.
Solution
This is possible, because you can instantiate a context with a constructor that accepts a connection string. In one of our projects we have a ContextFactory where the connection string is registered once at startup. All code in the assembly gets contexts from the factory.
Actually, in our case we use an ObjectContext, and create the connection string for the factory from the connection that is registered and the metadata string that was created when creating the context. We do this by using an EntityConnectionStringBuilder.