4

I am looking for some advice on how to reduce my Entity Framework library's dependency on app or web.config files. My app containins an ADO.NET Entity Data Model.

I currently reference this library in a web project, but in order to do so, I must add the connection string to the web app's web.config before deploying.

I now have a requirement to use the standalone DLL and am unable to alter the parent project's configuration files to include that of my EF library.

What would be the best way to store my connection string, yet maintain the integrity of my edmx and where should I reference it?

Nick
  • 5,844
  • 11
  • 52
  • 98
  • This question may help: http://stackoverflow.com/questions/161763/putting-configuration-information-in-a-dll – Rich O'Kelly Jan 09 '12 at 11:40
  • 1
    You should have the connection string in your app.config and the DLL that you are reference should have a property which you can GET and SET from the calling program. – Ash Jan 09 '12 at 11:43
  • @AshwaniRoy I don't want the calling program to have any control over the connection string. – Nick Jan 09 '12 at 11:44
  • @rich.okelly I have tried this, however despite the File.dll.config file being present in the output directory, I receive errors when running the application. 'The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.' – Nick Jan 09 '12 at 11:45
  • @Nick If the config is called File.dll.config, the code requesting the connection string must be in File.dll, is this the case in your app? – Rich O'Kelly Jan 09 '12 at 11:52
  • File.dll is my entity framework project. I have a parent DLL which calls methods within File.dll – Nick Jan 09 '12 at 11:56
  • Do you say that requirement is to hardcode connection string into your .dll? That is the most stupid requirement you can ever get. I would not do that. – Ladislav Mrnka Jan 09 '12 at 13:09
  • @LadislavMrnka No, this is not my requirement. I want my library to manage the connection string. The calling application does not care which database the information came from. I am not suggesting "hard coding" it as such, but in my current configuration, the executing application must specify the connection string in its own web.config which, in this case, is not possible. – Nick Jan 09 '12 at 13:15

2 Answers2

7
EntityConnectionStringBuilder builder = New EntityConnectionStringBuilder();
builder.Metadata =
    "res://*/Entities.VDMFinance.csdl|res://*/Entities.YourEntities.ssdl|res://*/Entities.YourEntities.msl";
builder.Provider = "System.Data.SqlClient";
builder.ProviderConnectionString = yourConnectionString;
Dim dc As New YourEntities(builder.ConnectionString)
dc.CommandTimeout = 0;

You can initialize all your data contexts from static factory that does this all the time. You can get the metadata and provider from the initial connection string that gets generated when you create the entities.

Yuck
  • 49,664
  • 13
  • 105
  • 135
linkerro
  • 5,318
  • 3
  • 25
  • 29
  • Thanks for the tip. Could you advise how I should tell my entity framework to use this as my connection string definition? – Nick Jan 09 '12 at 11:58
  • @Nick You tell EF to use this by passing it in to the constructor for your data context. This is happening in the next to last line of th example (above). – John Laffoon Jan 09 '12 at 13:30
3

Specifying configuration in the main web.config / app.config is common for all .NET applications. Even large products use it. Trying to avoid it doesn't have any benefit.

If you want your .dll to manage connection string it must read it from somewhere - probably from custom configuration file with file name hardcoded in your library. Once you have connection string loaded you can pass it to constructor of your ObjecContext as @linkerro showed.

Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670