MSDN Documentation

.NET Concepts: Data Access

Entity Client Provider

The Entity Client provider is a .NET data provider that enables you to access data using the Entity SQL language. It works with the Entity Framework, allowing you to query and manipulate data in an object-oriented manner while leveraging the power and flexibility of SQL-like syntax.

Introduction

The Entity Client provider abstracts the underlying data source and provides a consistent interface for interacting with data using Entity SQL. It's particularly useful when you need fine-grained control over your queries or when working with scenarios where the Object Services layer of the Entity Framework might be too high-level.

Key Features

Using the Entity Client Provider

Here's a basic example of how to use the Entity Client provider to query data:


using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Common;

// Assume you have an Entity SQL connection string
// Example: "metadata=res://*/;provider=System.Data.SqlClient;provider connection string='Data Source=your_server;Initial Catalog=your_database;Integrated Security=SSPI;'"
string connectionString = "your_entity_sql_connection_string";

using (EntityConnection entityConnection = new EntityConnection(connectionString))
{
    entityConnection.Open();
    Console.WriteLine("Connection opened successfully.");

    string entitySqlQuery = "SELECT VALUE c FROM YourModelContainer.Customers AS c WHERE c.City = 'London'";

    using (EntityCommand entityCommand = entityConnection.CreateCommand())
    {
        entityCommand.CommandText = entitySqlQuery;
        entityCommand.CommandType = CommandType.Text;

        using (EntityDataReader reader = entityCommand.ExecuteReader(CommandBehavior.SequentialAccess))
        {
            while (reader.Read())
            {
                // Access data using reader.GetValue(columnIndex) or reader.GetString(columnIndex), etc.
                // The specific properties available depend on your Entity SQL query and model.
                Console.WriteLine($"Customer ID: {reader["CustomerID"]}, Name: {reader["FirstName"]}");
            }
        }
    }
}
            

The EntityConnection Class

The EntityConnection class represents a connection to a data source using Entity SQL. It requires a connection string that specifies the metadata information and the underlying provider connection string.

The EntityCommand Class

The EntityCommand class is used to execute Entity SQL commands against the data source. You set the CommandText property to your Entity SQL query.

The EntityDataReader Class

EntityDataReader provides a forward-only, read-only stream of data from the data source, similar to SqlDataReader or DbDataReader.

Relationship with Entity Framework

While the Entity Client provider can be used independently, it's tightly integrated with the Entity Framework. The Entity Framework's Object Services use the Entity Client provider under the hood to interact with the data source when you execute LINQ to Entities queries. You can also access the underlying EntityConnection and EntityCommand from an ObjectContext if you need to execute raw Entity SQL.

Note: For most common scenarios, using LINQ to Entities with the Entity Framework is the recommended approach. The Entity Client provider is more for advanced usage or when direct Entity SQL manipulation is required.

Best Practices

See Also