Database Reference
In-Depth Information
13-2. Retrieving a Single Entity Using an Entity Key
Problem
You want to retrieve a single entity using an entity key, regardless of whether you're implementing a Database-First,
Model-First, or Code-First approach for Entity Framework. In this example, you want to implement the Code-First
approach.
Solution
Suppose that you have a model with an entity type representing a painting. The model might look like the one shown
in Figure 13-2 .
Figure 13-2. The Painting entity type in our model
To start, this example leverages the Code-First approach for Entity Framework. In Listing 13-2, we create the
entity class, Painting.
Listing 13-2. The Painting Entity Object
public class Painting
{
public string AccessionNumber { get; set; }
public string Name { get; set; }
public string Artist { get; set; }
public decimal LastSalePrice { get; set; }
}
Next, in Listing 13-3, we create the DbContext object, which is our gateway into Entity Framework functionality
when leveraging the Code-First approach.
Listing 13-3. DbContext Object
public class Recipe2Context : DbContext
{
public Recipe2Context()
: base("Recipe2ConnectionString")
 
Search WWH ::




Custom Search