Database Reference
In-Depth Information
Chapter 7
Working with Object Services
This chapter contains a rather eclectic collection of recipes that provide practical solutions to common problems in
real-world applications. We build our applications to tolerate changes in deployment environments, and we make our
applications flexible enough so that few if any configuration details need to be hard-coded.
The first three recipes provide you with the tools to meet these challenges. The remaining recipes cover topics
such as Entity Framework's Pluralization Service, using the edmgen.exe utility, working with identifying relationships,
and retrieving objects from an object context.
7-1. Dynamically Building a Connection String
Problem
You want to build the connection string dynamically for your application.
Solution
Many real-world applications start out on a developer's desktop; move through one or more testing, integration,
and staging environments; and finally end up in a production deployment. You want to configure the application's
connection string dynamically depending on the current environment.
To build the connection string dynamically for your application, follow the pattern in Listing 7-1.
Listing 7-1. Dynamically Building a Connection String
public static class ConnectionStringManager
{
public static string EFConnection = GetConnection();
private static string GetConnection()
{
var sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = ConfigurationManager.AppSettings["SqlDataSource"];
// fill in the rest
sqlBuilder.InitialCatalog = ConfigurationManager.AppSettings["SqlInitialCatalog"];
sqlBuilder.IntegratedSecurity = true;
sqlBuilder.MultipleActiveResultSets = true;
 
Search WWH ::




Custom Search