Entity Framework with Connection String in Seperate Project

HI all,

I'm after some best practise advice here.

I currently has an Entity Framework edmx held in a seperate project. The connection string is held in the app.config of this project.

Now when I try and create an instance of myDbContext() from the seperate project I get the message that the connection string does not exist. From googling this appear to be because it looks for the connection string in the local project.

Coudl someone please help me find a solution where I can (maybe by using a helper method). Keep the connection string contained in the Entity project? i.e. so I can just call the dbCOntext without having to have multiple connection strings across my projects?

Thansk in advance.

Matt
flynnyAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
I would never hard code a connection string into compiled code. What if you need to quickly change it? You would need to fix, recompile, redeploy.

Keeping your data access in its own component is the way to go. But you could have many environments (Dev, QA, Test, training, Prod, ...) each of which having its own connection string. Have a look at http://emoreau.com/Entries/Articles/2009/05/Using-the-ConnectionStrings-section-of-the-configuration-file.aspx
0
 
Éric MoreauSenior .Net ConsultantCommented:
If you have a separate, that means that it is a .Dll. The App.Config file of a class library is created by the designer because it doesn't where else to put stuff. But when your class library is used by an application, the App.Config of that application needs to have everything in it (all others are just ignored).
0
 
flynnyAuthor Commented:
Hi Eric,

Thanks for your reply.

Sorry, I understand why the app.config file is not reading the connection string, however, is it possible for me to somehow reference a single connection string (maybe from a helper method) and pass it through to my context?

Like I said I am looking at best practise and open to suggestions, basically I would rather just have a single connection string entry rather than have to woory about multiple connections string in each project access in the entity solution.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Éric MoreauSenior .Net ConsultantCommented:
I don't understand why you would have multiple connection strings.

The designer will create one and put it in the App.Config of your class library. You then need to copy this section to your application's own App.Config and you are good to go.

Do you have multiple .EXE projects all accessing the same EF class library? This is the only time I could think where you get multiple App.Config/connection strings.
0
 
it_saigeDeveloperCommented:
Hi Flynny,

Is your connection string ever going to change or will it have the capability of being changed?

-saige-

P.S. --> What EF version are we talking about here?
0
 
it_saigeDeveloperCommented:
One other question, are the csdl, ssdl and msl a part of the same library?  (meaning that the library you have the connection string defined in, is the model a part of that same library).

If it is, I am assuming that the model will be referenced (or should be referenceable) from external assemblies.

-saige-
0
 
flynnyAuthor Commented:
Hi Guys,

Thanks for the help guys and sorry for the late reply

Basically, I was hoping to have some kind single entry point so if the db connections ever did change I would only have to change one point.

I have a number of solutions using the entity solution, all different functions.

I have a helper functions solution. Would it be best practise to maybe store the db connection in a static string in here and ref it when creating a context in my other solutions?

Also would it be possible to have the entity framework solution refrence it rather than the one in its config file?
0
 
Éric MoreauSenior .Net ConsultantCommented:
EF designers updates the config file in the project it is living because it doesn't know which other project to affect. Your connection strings surely don't change often enough to be a real show stopper.
0
 
flynnyAuthor Commented:
Hi Eric,

No its not, I just wanted to try and alleviate as many issues as possible and keep it simple.

Realistically they will not change either. The idea is mainly to try and keep the db access seperate from the other projects. Also is the connection string is stored in  DLL rather than a config file the user password details are kept away from the main project (or am I wrong does this get pulled away at compile time?).

Whilst this isnt an issue currently we are just trying to plan for the future.

So realistically, I would keep the app.config connection string in the ef project. Then I coud use a static kept string to use when the projects access the db and create the dbcontext?
0
 
flynnyAuthor Commented:
Thanks for your help Eric. Its great to get well explained and reasoned answers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.