The code

We won’t be workiing directly with the XML (or at least not any more than we can help), but we will be working with the code files that the Wizard generates. When you create a model using the Model-First workflow, the Wizard creates a single file that contains partial class definitions for the entity objects in the model. We’ll be looking at the Entity Framework object model in detail in Chapter 7 (so don’t worry too much about learning all these details now), but here’s a quick peak:

The context

After the library imports (using in C#, imports in VB) and some metadata declarations, the file contains the definition for the context object. The partial class contains the following members:

Image Two constructors. One allows you to pass just the name of the instance; the other supports both the name and a connection string.

Image The OnContextCreated() method. This is declared as an partial method that doesn’t do anything, but you can redefine it in a code-behind file. (We’ll see how in Chapter 7.)

Image ObjectSet definitions. Every entity in the model is represented by an instance of ObjectSet<TEntity>, which is a queryable collection of the entity instances.

Image AddTo() methods. The code generated by the designer also defines a set of AddTo() methods (e.g., AddToRecipes()). These methods are perfectly usable, but they’ve been replaced by Add() methods on the entity definitions themselves, and their use is deprecated.


Image Make A Note

The code the designer generates can be controlled using a T4 template. T4 isn’t really part of Entity Framework, but we’ll take a quick look at how that process works in Chapter 7.


The entity classes

After the context is declared, the code contains partial class definitions for each entity class in the model. (The ObjectSet<T> properties in the context contain instances of these types.) Each entity contains the following members:

Image A Factory method. Rather than a constructor, the entities are defined using a factory method (e.g., CreateRecipe()) that accepts any required (non-null) properties as parameters.

Image Property definitions. The partial class contains property definitions for each property of the entity. The scalar properties are in the “Primitive Properties” region, and the association properties (i.e., those that represent relationships between entities) are in the “Navigation Properties” region.)

The scalar property set methods are wrapped in calls to PropertyChange and ReportPropertyChange events.


Image On Your Own

Open up the Hello.designer.cs or Hello.designer.vb file and have a poke through it. Can you answer the following questions?

Because OnContextCreated() is defined as partial, you can override it. Can you think of any situations in which you might want to do that? Why?

Why do you think the designer creates factory methods instead of constructors?

The ObjectSet<T> definitions set LazyLoadingEnabled true.  This property controls how the Entity Framework retrieves data from the database. What do you think it does?


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset