EntityClient Provider

The EntityClient provider is a .NET data provider that enables you to access data from an Entity Framework conceptual model using Entity SQL. It acts as a bridge between your application and the underlying data source, abstracting the complexities of direct database interaction.

Overview

EntityClient is designed to work with the Entity Framework. It allows you to query your entities using Entity SQL, a powerful declarative query language that is independent of the underlying store's query language (like T-SQL or PL/SQL). This independence makes your applications more portable and resilient to database changes.

Key Features

How it Works

When you use the EntityClient provider, you typically:

  1. Define a connection string that includes the path to your EDM metadata.
  2. Create an instance of the System.Data.EntityClient.EntityConnection class.
  3. Create an instance of the System.Data.EntityClient.EntityCommand class, specifying an Entity SQL query.
  4. Associate the command with the connection.
  5. Execute the command to retrieve data as an EntityDataReader.

Example Usage

The following code snippet demonstrates how to use the EntityClient provider to query data:


using System;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;

public class EntityClientExample
{
    public static void Main(string[] args)
    {
        // Connection string includes the path to your EDM file (e.g., .edmx)
        string connectionString = @"metadata=res://*;provider=System.Data.SqlClient;provider connection string=""Data Source=.\SQLEXPRESS;Initial Catalog=AdventureWorksLT;Integrated Security=True;MultipleActiveResultSets=True""";

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

                // Entity SQL query
                string entitySql = "SELECT VALUE c FROM AdventureWorksLTEntities.Customers AS c WHERE c.FirstName = 'Michael'";

                using (EntityCommand command = new EntityCommand(entitySql, connection))
                {
                    using (EntityDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        while (reader.Read())
                        {
                            Console.WriteLine($"CustomerID: {reader["CustomerID"]}, FirstName: {reader["FirstName"]}, LastName: {reader["LastName"]}");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
        }
    }
}
            
Note: The connection string format and the Entity SQL query depend on your specific EDM and data source.

Entity SQL vs. LINQ to Entities

While EntityClient allows direct use of Entity SQL, the Entity Framework also provides LINQ to Entities, which offers a more strongly-typed and familiar querying experience for .NET developers. LINQ to Entities queries are translated into Entity SQL by the framework before being executed by EntityClient.

When to Use EntityClient Directly:

See Also