Export (0) Print
Expand All

EntityClient and Entity SQL

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

Internally, the Entity Framework uses the System.Data.EntityClient provider to access data. The EntityClient provider exposes generic interfaces for client code and acts as a bridge to other .NET Framework data providers, such as System.Data.SqlClient. If you have to use EntityClient directly, it is easy to code against because it follows the familiar programming patterns found in the other .NET Framework data providers.

EntityClient may be a good solution if you do not need the full object materialization provided by Object Services or if your application is designed to consume a forward-only stream of tabular data. You can use the following EntityClient provider classes as you would use the SqlClient provider:

  • EntityConnection encapsulates a connection to an underlying database and a metadata workspace.

  • EntityCommand executes commands against the data source.

  • DbDataReader reads a forward-only stream of rows from a database.

The Entity Framework uses Entity SQL as the query language for the EntityClient provider and in the object services component. Entity SQL is a storage-independent extension of SQL that provides support for Entity Data Model (EDM) constructions such as entities and relationships.

An EntityConnection connection string contains the underlying .NET Framework data provider name, the provider connection string to the data source, and the path to the metadata (.csdl, .ssdl, and .msl files). An EntityCommand executes commands against a data model. This example shows how to create an EntityCommand directly by opening an EntityConnection and specifying the Entity SQL query. You must set the CommandBehavior to SequentialAccess for the DbDataReader when you execute an EntityCommand.

// Connection string specifies the data source and mapping.
string connectionString =
    "Metadata=.\\School.csdl|.\\School.ssdl|.\\School.msl;" +
    "Provider=System.Data.SqlClient;Provider Connection String=\"" +
    "Data Source=localhost;Initial Catalog=School;Integrated Security=True\"";

try
{
    using (EntityConnection connection = 
        new EntityConnection(connectionString))
    {
        connection.Open();
        EntityCommand command = new EntityCommand(
            "SELECT p.LastName, p.FirstName " +
            "FROM SchoolEntities.Person AS p " +
            "ORDER BY p.LastName",
            connection);
        // Entity command requires SequentialAccess
        DbDataReader reader = command.ExecuteReader(
            CommandBehavior.SequentialAccess);
        while (reader.Read())
        {
            Console.WriteLine("{0}\t{1}", 
                reader[0], reader[1]);
        }
    }
}
catch(QueryException ex)
{
    Console.WriteLine(ex.ToString());
}

You can use parameters with an EntityCommand by creating them through the Parameters collection using AddWithValue to supply the ID value. Note that parameter names should not include the @ sign when they are being added to the Parameters collection.

// For demonstration purposes the id is a hard-coded value.
int id = 1;

// Connection string specifies the data source and mapping.
string connectionString =
    "Metadata=.\\School.csdl|.\\School.ssdl|.\\School.msl;" +
    "Provider=System.Data.SqlClient;Provider Connection String=\"" +
    "Data Source=localhost;Initial Catalog=School;Integrated Security=True\"";

try
{
    using (EntityConnection connection = 
        new EntityConnection(connectionString))
    {
        connection.Open();
        EntityCommand command = new EntityCommand(
        "SELECT p.LastName, p.FirstName " +
        "FROM SchoolEntities.Person AS p " +
        "WHERE p.PersonID = @id",
        connection);

        command.Parameters.AddWithValue("id", id);
        DbDataReader reader = command.ExecuteReader(
            CommandBehavior.SequentialAccess);
        while (reader.Read())
        {
            Console.WriteLine("{0}\t{1}", 
                reader[0], reader[1]);
        }
    }
}
catch (QueryException ex)
{
    Console.WriteLine(ex.ToString());
}

For more information about Entity SQL queries, see Entity SQL How-to Topics.

Community Additions

Show:
© 2014 Microsoft