Online Training for MVC Web Designing Development, MS SQL, Digital Marketing

Connection string builder in Entity Framework

Decrypt an encrypted string value in entity framework connection builder

Here you learn how to set up database connection information in entity framework connection string builder.

In DbContext constructor you can simply pass your connection string, but that may look dirty and will be difficult to maintain when any changes required in future.

So, we create a separate class with static string property
Entity Connection StringBuilder setup

Note: In case you have encrypted username and password or other information, you can decrypt them before setting up into Entity Connection String Builder object.

This is how you can set create a property to setup connection details, provider and metadata information, read the code here.
entity framework databse connection setup

If you are not sure about what .csdl .ssdl file names are! Check your edmx file
Here is the Code for SqlConnectionStringBuilder setup
public static string EFConnectionString
SqlConnectionStringBuilder providerCs = new SqlConnectionStringBuilder();
providerCs.InitialCatalog = ConfigurationManager.AppSettings["DatabaseName"];
providerCs.UserID = ConfigurationManager.AppSettings["UserId"];
providerCs.Password = ConfigurationManager.AppSettings["Password"];
providerCs.DataSource = ConfigurationManager.AppSettings["ServerName"];

//providerCs.UserID = CryptoService2.Decrypt(ConfigurationManager.AppSettings["UserId"]);

providerCs.MultipleActiveResultSets = true;
providerCs.TrustServerCertificate = false;

EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Provider = "System.Data.SqlClient";
ecsb.ProviderConnectionString = providerCs.ToString();

ecsb.Metadata = string.Format("res://{0}/EDModel.csdl|res://{0}/EDModel.ssdl|res://{0}/EDModel.msl", typeof(Entities).Assembly.FullName);
return ecsb.ToString();

entity framework connection string builder

Group Training