Compartir a través de


QueryView Element (EntitySetMapping)

In the Entity Data Model (EDM), the QueryView element in an EntitySetMapping defines a read-only mapping between an entity in the conceptual model and an entity in the storage model. You define this query view mapping with an Entity SQL query that is evaluated against the storage model, and you express the result set in terms of an entity in the conceptual model. Because query views are read-only, you cannot use standard update commands to update types that are defined by query views. You can make updates to these types by using modification functions. For more information, see How to: Map Modification Functions to Stored Procedures.

You can define query views to enable the following scenarios:

  • Define an entity in the conceptual model that doesn't include all the properties of the entity in the storage model. This includes properties that do not have default values and do not support null values.

  • Map computed columns in the storage model to properties of entity types in the conceptual model.

  • Define a mapping where conditions used to partition entities in the conceptual model are not based on equality. When you specify a conditional mapping using the Condition element, the supplied condition must equal the specified value. For more information, see Condition Element (MappingFragment).

  • Map the same column in the storage model to multiple types in the conceptual model.

  • Map multiple types to the same table.

  • Define associations in the conceptual model that are not based on foreign keys in the relational schema.

  • Use custom business logic to set the value of properties in the conceptual model. For example, you could map the string value "T" in the data source to a value of true, a Boolean, in the conceptual model.

  • Define conditional filters for query results.

  • Enforce fewer restrictions on data in the conceptual model than in the storage model. For example, you could make a property in the conceptual model nullable even if the column to which it is mapped does not support null values.

The following considerations apply when you define query views for entities:

  • Query views are read-only. You can only make updates to entities by using modification functions.

  • When you define an entity type by a query view, you must also define all related entities by query views.

  • When you map a many-to-many association to an entity in the storage model that represents a link table in the relational schema, you must define a QueryView element in the AssociationSetMapping element for this link table. For more information, see QueryView Element (AssociationSetMapping).

  • Query views must be defined for all types in a type hierarchy. You can do this in the following ways:

    • With a single QueryView element that specifies a single Entity SQL query that returns a union of all of the entity types in the hierarchy.

    • With a single QueryView element that specifies a single Entity SQL query that uses the CASE operator to return a specific entity type in the hierarchy based on a specific condition.

    • With an additional QueryView element for a specific type in the hierarchy. In this case, use the TypeName attribute of the QueryView element to specify the entity type for each view.

  • When a query view is defined, you cannot specify the StorageSetName attribute on the EntitySetMapping element.

  • When a query view is defined, the EntitySetMapping element cannot also contain Property mappings.

The following example defines a query view for the Product entity type in the AdventureWorks Sales model.

<EntitySetMapping Name="Product">
  <QueryView>
      SELECT VALUE AdventureWorksModel.Product(P.ProductID,
      P.Name, P.ProductNumber, P.Color, P.ListPrice, P.Size, P.Weight,
      P.Style)
      FROM Person.Product as P
  </QueryView>
</EntitySetMapping>

Because the query only returns a subset of the members of the Product type in the storage model, the Product type in the AdventureWorks Sales model has been modified based on this mapping as follows:

  <EntityType Name="Product">
  <Key>
    <PropertyRef Name="ProductID" />
  </Key>
  <Property Name="ProductID" Type="Int32" Nullable="false" />
  <Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
  <Property Name="ProductNumber" Type="String" Nullable="false" MaxLength="25" Unicode="true" FixedLength="false" />
  <Property Name="Color" Type="String" MaxLength="15" Unicode="true" FixedLength="false" />
  <Property Name="ListPrice" Type="Decimal" Nullable="false" Precision="19" Scale="4" />
  <Property Name="Size" Type="String" MaxLength="5" Unicode="true" FixedLength="false" />
  <Property Name="Weight" Type="Decimal" Precision="8" Scale="2" />
  <Property Name="Style" Type="String" MaxLength="2" Unicode="true" FixedLength="true" />
</EntityType>

The Product type in the storage model of the AdventureWorks Sales model sample was not changed for this example.

See Also

Concepts

Entity Data Model Mapping Scenarios (Application Scenarios)
EntitySetMapping Element (MSL)