Database Reference
In-Depth Information
var eBuilder = new EntityConnectionStringBuilder();
eBuilder.Provider = "System.Data.SqlClient";
eBuilder.Metadata =
"res://*/Recipe1.csdl|res://*/Recipe1.ssdl|res://*/Recipe1.msl";
eBuilder.ProviderConnectionString = sqlBuilder.ToString();
return eBuilder.ToString(); }
}
public partial class EF6RecipesContainer
{
public EF6RecipesContainer(string nameOrConnectionString)
: base(nameOrConnectionString)
{
}
}
How It Works
When you add an ADO.NET Entity Data Model to your project, Entity Framework adds an entry to the
<connectionStrings> section in your project's .config file. At runtime, the constructor for the object context is passed
the key for this configuration entry (EF6RecipesContext for many of the recipes in this topic). Given this key, the
database context uses the connection string found in the .config file.
To create the connection string dynamically based on the environment in which our application is
deployed, we created the ConnectionStringManager class (refer to Listing 7-1). In the GetConnection() method,
we obtain the environment-specific values for data source and initial catalog from a config file. To use our
ConnectionStringManager, we implemented an additional constructor that takes a string parameter representing the
connection string or name inside the EF6RecipesContainer partial class.
When we instantiate EF6RecipesContainer, we can pass into it the value of ConnectionStringManager.EFConnection
and, as a result, the instance will use the dynamically created connection string to connect to the database server.
7-2. Reading a Model from a Database
Problem
You want to read the CSDL, MSL, and SSDL definitions for your model from a database table.
Solution
Suppose that you have a model like the one shown in Figure 7-1 .
Figure 7-1. A model with a Customer entity
 
Search WWH ::




Custom Search