Database Reference
In-Depth Information
Figure 1-3. A model with Employee and Task with a one-to-many association between them
An EntityType usually has one or more properties . Just like with a class, a property is a named value with a specific
data type. Properties can have simple types like integer, string, and so on; or have ComplexTypes; or be collections.
Navigation properties refer to other related entities (typically represented by foreign key relationships in a database).
The non-navigation properties on an EntityType are usually just called scalar properties .
A relationship between two entities is called an association . Associations between EntityTypes are shown on the
design surface as a line connecting the EntityTypes. The line is annotated to show the multiplicity on each end of the
association. The association in Figure 1-3 is a one-to-many association between Employee and Task. An Employee
can have zero or more tasks. Each Task is associated to exactly one Employee.
Every EntityType has a property or set of properties that denote its EntityKey . An EntityKey uniquely identifies
the entity to Entity Framework and is most often mapped to a primary key from the entity's representation in the
underlying database.
Finally, no discussion on Entity Framework would be complete without mentioning the context object . The
context object for Entity Framework is your gateway into the Entity Framework services. The context object exposes
entity objects, manages the database connection, generates parameterized SQL, marshals data to and from the
database, caches objects, helps maintain change tracking and materializes , or transforms, an untyped result set into a
collection of strongly typed objects.
In the beginning, there was the ObjectContext object. Now, Entity Framework supports an alternate, more
streamlined context object called the DbContext . The DbContext greatly simplifies the developer experience when
working with Entity Framework. Interestingly, the DbContext is a wrapper, or facade, around the ObjectContext,
exposing the underlying ObjectContext functionality in an intuitive, friendly and productive way.
Clearly, the DbContext is the preferred approach for working with Entity Framework as we will demonstrate in
great detail in this topic.
The Code
Despite a tremendous emphasis on visual design support, the Entity Framework is all about code. The models,
EntityTypes, associations, mappings, and so on are ultimately expressed in concrete code that becomes part of
your application. This code is either generated by Visual Studio and Entity Framework or created manually by the
development team. You can choose quite a bit about the code-generation process or the lack of it by changing various
properties on your project or modifying the underlying code-generation templates.
Visual Studio uses a code-generation technology called Text Template Transformation Toolkit , simply referred
to as T4 templates . The Visual Studio tooling uses T4 templates to generate, or scaffold , code automatically. The great
thing about T4 template support in Visual Studio is that you can edit the templates to tailor the code-generation
process to match your exact needs. This is an advanced technique, but it is necessary in some cases. We'll show you
how to do this in a few recipes.
 
Search WWH ::




Custom Search