Export (0) Print
Expand All

Conceptual Model Functions (Entity Data Model)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

The Entity Data Model (EDM) supports functions that are declared in conceptual schema definition language (CSDL). Conceptual model functions provide an alternative to mapping stored procedures and storage model functions. The declaration of a conceptual model function uses standard CSDL syntax. The function definition uses the Entity SQL Language. By using conceptual model functions, you can create methods that act on data without mapping to a procedure in the database management system or other data source. The Entity SQL language executes the action from the definition that is provided in CSDL.

The following example shows basic schema syntax and the ReturnType, Parameter, and DefiningExpression elements that are used to define a conceptual model function.

<Function Name="ShippingDelay" ReturnType="Edm.Int32">
    <Parameter Name="Order" Type="Model.Order" />
    <DefiningExpression>
        CurrentDateTime() - Order.Date
    </DefiningExpression>
</Function>

This function takes an Order type parameter, extracts the date of the Order, and then calculates the difference between Order.Date and the date calculated by the CurrentDateTime method. The difference is returned to the caller as an Edm.Int32.

Conceptual model functions are declared as global items inside the Schema element. The DefiningExpression of a conceptual model function is written in Entity SQL. Functions without a DefiningExpression are simply declarations. Unlike functions in SSDL, functions in CSDL only support in-bound parameters, so the Mode attribute of a parameter is always: Mode="In". Neither CSDL nor SSDL functions can be used inside another function as input for the outer function, that is, these functions are not composable.

Function parameters and return types can be scalar, entity, complex, row, or REF types, or a collection of one of those types.

Parameter Type

The type of a parameter can be declared either as an attribute:

<Parameter Name="Age" Type="Edm.Int32"/>

Or inline:

<Parameter Name="Owner">
    <TypeRef Name="Model.Person" />
</Parameter>

Return Type

The return type of a function can be declared either as an attribute :

<Function Name="GetAge" ReturnType="Edm.Int32">

Or inline:

<Function Name="GetAge">
    <ReturnType Type="Edm.Int32" />
</Function>

Collection Type

If the type of a parameter, property, or return type of a function is a collection, the type of the collection can be expressed either as an attribute or as a CollectionType child element.

You can only define the type of a collection as an attribute when the element type of the collection is named, or, in other words, if the element type is not a RowType. The following example shows a collection type defined as an attribute:

Type="Collection(Model.Person)")

The following example uses a child element to specify the type that the collection holds. This syntax can be used whenever it is possible to reference a CollectionType:

<Parameter Name="Owners">
    <CollectionType>
        <TypeRef Name="Model.Person" />
    </CollectionType>
</Parameter>

RowType

A RowType is similar to a ComplexType with the following distinctions:

A RowType is unnamed, so it needs to be declared inline.

The Type of a Property belonging to a RowType can be an EntityType, RefType, RowType, or CollectionType.

The following example shows the definition of a RowType.

<RowType>
    <Property Name="X" Type="int" Nullable="false"/>
    <Property Name="Y" Type="int" Nullable="false"/>
    <Property Name="Z" Type="int" Nullable="false"/>
</RowType>

A RowType is used inline in a Parameter or ReturnType definition:

Parameter definition:

<Parameter Name="Coordinate" Mode="In">
    <RowType>
        <Property Name="X" Type="int" Nullable="false"/>
        <Property Name="Y" Type="int" Nullable="false"/>
        <Property Name="Z" Type="int" Nullable="false"/>
    </RowType>
</Parameter>

ReturnType definition:

<Function Name="GetFriends" EntitySet="People" >
    <ReturnType>
        <CollectionType>
            <RowType>
                <Property Name="X" Type="int" Nullable="false"/>
                <Property Name="Y" Type="int" Nullable="false"/>
                <Property Name="Z" Type="int" Nullable="false"/>
                </RowType>
        </CollectionType>
    </ReturnType>
<Parameter Name="Person" Type="Model.Person" />
<DefiningExpression>…</DefiningExpression>
</Function>

In the second example, the Collection element indicates that the function returns a collection of the RowType as defined inline.

ReferenceType

You can return a reference for a type by using Entity SQL. The following example selects a reference to a Customer type:

SELECT REF(C) FROM [MyContainer.Customers] AS C

To specify that a Collection or RowType property holds an entity reference instead of an entity, use the following syntax in the conceptual model:

<ReferenceType Type="Model.Person" />

If the above Entity SQL syntax is used as the DefiningExpression for a Function, the return type syntax would be as follows:

<ReturnType>
    <CollectionType>
        <ReferenceType Type="Model.Person" />
    </CollectionType>
</ReturnType>

A ReferenceType that is used in an element of a conceptual model function must point to an EntityType, because only entities have the necessary key property.

See Also

Community Additions

Show:
© 2014 Microsoft