Sometimes, there comes a scenario where you are using certain architecture for your work but due to some issue or for some enhancement in the system you need to use two different connection providers, one for a simple connection and other for entity framework.
In my case I am using LINQ to SQL and entity framework altogether for doing some work.
What is the need for creating connection string dynamically for entity framework?
The real need arises as .edmx entity framework data objects create different connection strings than the linq to sql or we simply use in web.config file that's why we need to generate dynamically by using built in classes in C#.
Simple Connection string:
In my case I am using LINQ to SQL and entity framework altogether for doing some work.
What is the need for creating connection string dynamically for entity framework?
The real need arises as .edmx entity framework data objects create different connection strings than the linq to sql or we simply use in web.config file that's why we need to generate dynamically by using built in classes in C#.
Simple Connection string:
- <add name="ConnectionString9" connectionString="server=ServerName;database=DatabaseName;uid=db_user_id;password=db_user_password; Connect Timeout=0; Max Pool Size=5000"/>
Entity Connection String:
- <add name="DBEntities" connectionString="metadata=res://*/DB.csdl|res://*/DB.ssdl|res://*/DB.msl;provider=System.Data.SqlClient;provider connection string="data sourceServerName;initial catalog=DB_Name;persist security info=True;user id=DBUserID;password=DBUserPassword;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
So let's start it to build the connection string dynamically to be used in project. Firstly, we add required namespaces to be used in method.
- using System.Configuration;
- using System.Data.SqlClient;
- using System.Data.Entity.Core.EntityClient;

Tôm MaưPosted Aug 24, 2018, 1:03 PM
Many thanks , but i got a error at EntityHelper ();. Did you miss the class ?
dinesh kumarPosted Sep 5, 2017, 6:40 AM
I am getting error . 'EntityHelper' is a namespace but is used like a type.
dinesh kumarPosted Sep 5, 2017, 6:07 AM
Pls attach source code.
Ammar ShaukatPosted Apr 2, 2016, 6:01 AM
I could't understand the example in what cases or case studies we need a dynamic connection string usually ?