Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reducing Entity Framework library's dependency on app.config

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?

like image 297
Nick Avatar asked Dec 28 '22 08:12

Nick


2 Answers

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.

like image 153
linkerro Avatar answered Jan 04 '23 22:01

linkerro


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.

like image 31
Ladislav Mrnka Avatar answered Jan 04 '23 22:01

Ladislav Mrnka