RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Introducing ADO.NET Entity Framework : Page 3

The ADO.NET Entity Framework is core technology in Microsoft's evolving data platform that bridges the gap between data structures and objects in your applications.

A Brief Look at a Schema File
While this article won't delve into customization of the EDM, it is good to have a basic understanding of the actual schemas in the EDMX file. It is also important to note that you will need to do manual editing of this file for particularly more complex scenarios.

If you were to open up the EDMX file in the XML Editor (right-click, choose Open With, and then select the XML Editor) you will see that the file contains three major sections.

The first section is Conceptual Models. The core of the Conceptual portion of the schema file is the EntityTypes, each of which represents a table in the database. EntityTypes contain properties that define the fields of the table. Here is a simple EntityType for Shippers. While the wizard uses the actual table names to name the EntityTypes, a customized Entity might use the name Shipper because the EntityType defines a single entity:

   <EntityType Name="Shippers">
       <PropertyRef Name=" ShipperID " />
     <Property Name="ShipperID" Type="Int32" 
       Nullable="false" />
     <Property Name="CompanyName" Type="String" 
       Nullable="false" MaxLength="40" />
     <Property Name="Phone" Type="String" 
       MaxLength="24" />
     <NavigationProperty Name="Orders" 
       FromRole="Shippers" ToRole="Orders" />
An EntityType requires a Key so that the change tracking and updating can occur. In this case the ShipperID property will also be the Key for the Shippers entity. Each property is described through a variety of facets such as MaxLength. The properties and constraints of the database schema have been replicated in the EntityType.

The NavigationProperty references another element of the conceptual layer called an association. An association defines the entities involved in a relationship and what their multiplicity is. The FK_Orders_Shippers association defines a one-to-many relationship between Shippers and Orders:

   <Association Name="FK_Orders_Shippers">
     <End Role="Shippers" 
       Multiplicity="0..1" />
     <End Role="Orders" 
       Multiplicity="*" />
After defining the association, the entity's NavigationProperty further defines the direction of the relationship. In the case of the Shippers entity, start with Shippers and navigate through the relationship to Orders.

The other two sections of the schema are the Storage Models and the Mappings. Storage Models are the representation of the database schema, while the mappings describe how to move between the entities and properties of the conceptual layer and the entities and properties of the storage layer. The storage layer schema helps the ADO.NET Entity Framework APIs interact with the database.

The Generated Classes
With these relationships defined, ADO.NET Entity Framework can easily deliver related data without requiring you to build complex queries. In addition, you use the relationships when you add, modify, or delete data.
In addition to the XML schema files, the wizard also generated classes that represent the entities defined by the conceptual layer.

If you choose "Show All Files" in the Solution Explorer, you will see a class file listed as a child of the EDMX file named Model.Designer. This file contains the classes that you will use in your code.

Author's Note: You will need to manually edit the EDMX file for more complex scenarios.

It is important to understand that these classes define schema only, not behavior. While they are partial classes and fully extensible, they are not meant to replace your application objects. The classes are simply a conduit between your data store and the tools you build to use that data—reports, object models, presentation layers, web sites, web services, etc.

Relationships and Associations
The relationships that the model defines are a critical part of the conceptual layer. Using these relationships, the ADO.NET Entity Framework can easily deliver related data without requiring you to build complex queries. In addition, you use the relationships when you add, modify, or delete data. You will see this in more detail later in this article.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date