CommandText (SSDL)

In the Entity Data Model, the CommandText element in storage schema definition language (SSDL) represents a query in the language that is used by the database management system or storage model. Both the DefiningQuery and CommandText elements can use Transact-SQL or some other query syntax that is native to the storage model. The CommandText element can also reference parameters. After you have added the CommandText element to the SSDL, the element works like a stored procedure in the data model. Create a FunctionImport in the conceptual model (CSDL), and you can map entity modification behaviors to it. Because the CommandText is a child element of the EntityContainer, it becomes part of the ObjectContext, so you can implement a helper function in a partial class and call the function from application code.

The following example shows a function definition as it would appear in SSDL. The CommandText element defines the function in Transact-SQL syntax.

    <Function Name="InsertContact" IsComposable="false">
        <CommandText>
            INSERT INTO Contact
              ([FirstName],
               [LastName],
               [Title],
               [AddDate],
               [ModifiedDate])
            VALUES
             (@FirstName,@LastName,@Title,GETDATE(),GETDATE())
              SELECT SCOPE_IDENTITY() as ContactID
        </CommandText>
             <Parameter Name="FirstName" Type="nchar" Mode="In"/>
             <Parameter Name="LastName" Type="nchar" Mode="In"/>
             <Parameter Name="Title" Type="nchar" Mode="In"/>
    </Function>

See Also

Reference

DefiningExpression

Other Resources

How to: Define Custom Functions in the Storage Model
How to: Add a Defining Query