Database Reference
In-Depth Information
Listing 6-28. Entity Set Mapping Using QueryView for the WebOrder Table
<EntitySetMapping Name="WebOrders">
<QueryView>
select value
EFRecipesModel.WebOrder(o.OrderId,
o.CustomerName,o.OrderDate,o.IsDeleted,o.Amount)
from EFRecipesModelStoreContainer.WebOrder as o
where (o.OrderDate > datetime'2007-01-01 00:00') ||
(o.OrderDate between cast('2005-01-01' as Edm.DateTime) and
cast('2007-01-01' as Edm.DateTime) and !o.IsDeleted) ||
(o.Amount > 800 and o.OrderDate &lt;
cast('2005-01-01' as Edm.DateTime))
</QueryView>
</EntitySetMapping>
How It Works
QueryView is a read-only mapping that can be used instead of the default mapping offered by Entity Framework.
When QueryView is inside of the <EntitySetMapping> tag of the mapping layer, it maps entities defined on the store
model to entities defined on the conceptual model. When QueryView is inside of the <AssociationSetMapping> tag,
it maps associations defined on the store model to associations defined on the conceptual model. One common use
of QueryView inside of an <AssociationSetMapping> tag is to implement inheritance based on conditions that are
not supported by the default condition mapping.
QueryView is expressed in Entity SQL. QueryView can query only entities defined on the store model.
Additionally, eSQL in QueryView does not support group by and group aggregates.
When entities are mapped using QueryView, Entity Framework is unaware of the precise implementation of
the mapping. Because Entity Framework does not know the underlying columns and tables used to create instances
of the entities, it cannot generate the appropriate store-level actions to insert, update, or delete the entities. Entity
Framework does track changes to these entities once they are materialized, but it does not know how to modify them
in the underlying data store.
The burden of implementing the insert, update, and delete actions falls onto the developer. These actions can be
implemented directly in the .edmx file or they can be implemented as stored procedures in the underlying database.
To tie the procedures to the actions, you need to create a <ModificationFunctionMapping> section. We did this in
step 4 using the designer rather than directly editing the .edmx file.
If an entity mapped using QueryView has associations with other entities, those associations, along with the
related entities, also need to be mapped using QueryView. Of course, this can become rather tedious. QueryView
is a powerful tool, but it can rapidly become burdensome.
Some of the common use cases for using QueryView are as follows.
1.
To define filters that are not directly supported, such as greater than, less than, and so on
2.
To map inheritance that is based on conditions other than is null, not null, or equal to
3.
To map computed columns or return a subset of columns from a table, or to change a
restriction or data type of a column, such as making it nullable, or to surface a string
column as integer
4.
To map Table per Type Inheritance based on different primary and foreign keys
5.
To map the same column in the storage model to multiple types in the conceptual model
6.
To map multiple types to the same table
 
Search WWH ::




Custom Search