Sunday, October 16, 2011

EF Query Parsing


The following table lists the time in milliseconds to execute with the NwindOrdersEdmCS.sln (or VB)
project cached Entity SQL and T - SQL queries that return the same amount of data. It ’ s reasonable for
Entity SQL queries to be slower to execute than T - SQL Queries, because T - SQL doesn ’ t require creating
and translating the CQT or handling hierarchical resultsets.
380

Text without Order Details 10 ms. 7 ms.
XML without Order Details 17 ms. 11 ms.
Text with Order Details 27 ms. 17 ms.
XML with Order Details 42 ms. 21 ms.
 
Entity SQL from ecmdNwind.CommandText

SELECT o, o.Order_Details FROM NorthwindEntities.Orders AS o
WHERE o.ShipCountry = @Country ORDER BY o.OrderDate DESC

T - SQL from ToTraceString()

SELECT
[Project1].[OrderID] AS [OrderID],
[Project1].[CustomerID] AS [CustomerID],
[Project1].[EmployeeID] AS [EmployeeID],
[Project1].[OrderDate] AS [OrderDate],
[Project1].[RequiredDate] AS [RequiredDate],
[Project1].[ShippedDate] AS [ShippedDate],
[Project1].[ShipVia] AS [ShipVia],
[Project1].[Freight] AS [Freight],
[Project1].[ShipName] AS [ShipName],
[Project1].[ShipAddress] AS [ShipAddress],
[Project1].[ShipCity] AS [ShipCity],
[Project1].[ShipRegion] AS [ShipRegion],
[Project1].[ShipPostalCode] AS [ShipPostalCode],
[Project1].[ShipCountry] AS [ShipCountry],
[Project1].[C1] AS [C1],
[Project1].[C2] AS [C2],
[Project1].[OrderID1] AS [OrderID1],
[Project1].[ProductID] AS [ProductID],
[Project1].[UnitPrice] AS [UnitPrice],
[Project1].[Quantity] AS [Quantity],
[Project1].[Discount] AS [Discount]
FROM ( SELECT
[Extent1].[OrderID] AS [OrderID],
[Extent1].[CustomerID] AS [CustomerID],
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[OrderDate] AS [OrderDate],
[Extent1].[RequiredDate] AS [RequiredDate],
[Extent1].[ShippedDate] AS [ShippedDate],
[Extent1].[ShipVia] AS [ShipVia],
[Extent1].[Freight] AS [Freight],
[Extent1].[ShipName] AS [ShipName],
[Extent1].[ShipAddress] AS [ShipAddress],
[Extent1].[ShipCity] AS [ShipCity],
381
[Extent1].[ShipRegion] AS [ShipRegion],
[Extent1].[ShipPostalCode] AS [ShipPostalCode],
[Extent1].[ShipCountry] AS [ShipCountry],
1 AS [C1],
[Extent2].[OrderID] AS [OrderID1],
[Extent2].[ProductID] AS [ProductID],
[Extent2].[UnitPrice] AS [UnitPrice],
[Extent2].[Quantity] AS [Quantity],
[Extent2].[Discount] AS [Discount],
CASE WHEN ([Extent2].[OrderID] IS NULL) THEN CAST(NULL AS int)
ELSE 1 END AS [C2]
FROM [dbo].[Orders] AS [Extent1]
LEFT OUTER JOIN [dbo].[Order Details] AS [Extent2]
ON [Extent1].[OrderID] = [Extent2].[OrderID]
WHERE [Extent1].[ShipCountry] = @Country
) AS [Project1]
ORDER BY [Project1].[OrderDate] DESC, [Project1].[OrderID] ASC, [Project1].[C2] ASC
Executing the preceding batch in SQL Server Management Studio sends an extraordinarily long
statement to generate a conventional LEFT OUTER JOIN that ’ s the same as ordinarily generated by:
T - SQL
SELECT o.*, d.*
FROM Orders AS o LEFT OUTER JOIN [Order Details] AS d ON d.OrderID = o.OrderID
WHERE ShipCountry = ‘USA’
ORDER BY o.OrderID DESC

Saturday, October 15, 2011

Entity - Relationship and EDM Terminology


Entity - Relationship and EDM Terminology

EF is the heir apparent to DataSet s as Microsoft ’ s preferred relational data management architecture. EF
is much more than a conventional object/relational mapping (O/RM) tool. EF maps the schema of an
underlying relational database to an Entity Data Model (EDM), which is based on Dr. Peter Chen ’ s Entity-
Relationship (E - R) model. Like ObjectSpaces, three XML files define the physical (relational), mapping and
conceptual (E - R) schemas of layers to make the transformation from the relational model to the EDM. An
SQL- like language called Entity SQL (also called eSQL) queries the EDM ’ s mapping provider and returns to
the DbDataReader a collection of DbDataRecord s by default. An Object Services layer enables Entity
SQL queries against ObjectQuery instances to return IQueryable collections of entities ( EntitySet s)
or anonymous types instead of DbDataRecord s. Substituting LINQ to Entities for Entity SQL queries
against ObjectQuery instances is an alternative method for returning EntitySet s or anonymous types.

Entities are “ things ” that can be distinctly identified, such as persons, organizations and events.
Documents representing objects, such as deeds, and transactions, sales orders, purchase orders,
invoices, and line items also are entities. EDM refers to entities as EntityType s, which the EF
documentation describes as “ abstract specifications for the details of a data structure in the
application domain. ” EDM uses table names for EntityType names, but the singular form is
preferred; fortunately, editing EntityType names in the graphical EDM Designer is easy.
Entity sets classify (contain) entities with common properties. EDM ’ s EntitySet s contain
EntityType s of the same type or a subtype. It ’ s a common practice to name EDM EntitySet s
with plural of the EntityType ; Dr. Chen uses the singular form. An EDM EntityContainer
represents a database, which is a collection of EntitySet s.
Entity primary keys uniquely identify each entity in an entity set. An entity primary key, which
EDM calls a Key , Key attribute , or Key value , may be a natural (semantically meaningful) or
surrogate (artificial) key, such as an auto - incrementing int identity or ROWGUIDCOL column.
Relationships represent associations between entities, which EDM calls Association s. Most
Association s are binary (between two different entities), but unary (between different
members of the same entity, such as an Employee who reports to another Employee as his or
her manager) and ternary (between three entities, such as Supplier - Product - OrderItem ).
Relationship sets are tuples of relationships. The mathematic definition of tuple is an ordered list of
objects of a specified type . IEnumerable < AnonymousType > collections returned by LINQ queries
are n - tuples, where n is the number of the projection ’ s members. A quadruple results from a
four - member projection. EDM calls the tuples AssociationSet s and names them from the
underlying foreign - key constraint name, typically FK_ Source Target for SQL Server where
Source is the EntityType name of the End that represents the 1 side of a 1:many relation and
Target is the many End .
Roles are the function that the entity performs in the relationship, such as Husband and Wife for
Person entities in a Marriage relationship. An EDM Association represents each associated
EntityType as an End that has a Role attribute.
Mappings specify the potential number of entities that can participate in the relationship, which
more commonly is called cardinality. Cardinality can be 1: n (one - to - many, such as order:
lineitems), m :1 (many - to - one, such as customer:orders) and m : n (many - to - many, such as
products:suppliers.) EDM substitutes a Multiplicity attribute for each End of an
Association : 1 represents that a single EntityType is required, 0...1 indicates that a single
EntityType is optional, and * represents many.
Attributes are functions that that map from an entity set to a value set ; for example an OrderDate
attribute maps to a Date/Time value set, which (for SQL Server) ranges from 1753 - 01 -
01T00:00:00 to 9999 - 12 - 31:23:59:59. EDM calls an attribute a Property , instead of a field or
column.

Comprehending Entity Framework Architecture and Components

The Entity Framework implements and exploits the Entity Data Model with the following four major
components:
Mapping files and EDM Designer . Three XML files manage mapping for the transition from
the physical (relational or store) schema to the conceptual (business - object - oriented) schema.
Defining mapping in XML files minimizes — but doesn ’ t eliminate in v1 — persistence - related
attributes in business - object classes and prevents the need for reflection at runtime. The
graphical EDM Designer autogenerates a single ModelName .edmx file and the top - level
ObjectContext and EntityName business - object classes, whose use is optional, in ModelName
.Designer.cs or ModelName .Designer.vb . Building the project creates the three mapping
XML files, ModelName .ssdl , ModelName.msl , and ModelName .csdl and stores them in the
assembly as resources.
EntityClient , Entity SQL, and Client Views . EntityClient is anADO.NET data provider
that processes entity queries written in an entity - enabled SQL dialect called Entity SQL to
generate Client Views with a DbDataReader . EntityClient emulates SqlClient ’ s
properties with EntityConnection , EntityCommand , EntityDataReader , and other
Entity... properties and objects. The syntax of Entity SQL queries is identical for all database
brands that have a storage - specific ADO.NET data provider, such as the current SqlClient
version. EntityClient hands off its Canonical Query Tree (CQT, an expression tree, sometimes
called a Canonical Command Tree or CCT) to the storage - specific data provider, which
translates the CQT to the RDBMS ’ s SQL dialect.
Object Services . Object Services is a layer over the EntityClient that provides an
ObjectQuery instance to execute Entity SQL statements that return strongly typed business
object instances instead of high - performance Client Views. The ModelName .Designer.cs or
ModelName .Designer.vb file defines the business classes at design time. Use of the Object
Services layer is optional.
LINQ to Entities Provider . LINQ to Entities is a domain - specific LINQ implementation that
uses an IQueryable < T > expression to generate an expression tree, which the EntityClient
translates to a CQT and passes to the storage - specific data provider. The storage - specific
provider returns a DbDataReader , which EntityClient coverts to a IEnumerable < T >
sequence for the data access or presentation layer. In this respect, LINQ to Entities parallels
LINQ to SQL ’ s query pipeline that uses expression trees to output T - SQL.
The first two of the preceding components are common to the Entity Data Model in its role as a
foundation for future members of the nascent Entity Data Platform.