When generating an Entity Data Model using the EDM wizard, the wizard creates classes for all selected tables that represent entities. It also creates a descendant of System.Data.Objects.ObjectContext class, which controls the connection to the Advanced SQL Engine, and the whole data flow. This class includes properties and methods named after the SQL objects. These members are used to retrieve and modify data in the context.
The examples contained in this article all refer to the EDM called EDMExample. The EDM contains the following entities:
The following is an example that uses Linq To Entities to retrieve the OrderDate from Orders and the ProductID and UnitPrice from OrderDetails. The properties that are retrieved are written out to the console.
Entity Provider Example 1 |
Copy Code |
---|---|
public void EntityProviderExample1() { // Setting up the connection string to the data provider TdConnectionStringBuilder tbuilder = new TdConnectionStringBuilder(); tbuilder.Database = "Database"; tbuilder.UserId = "user"; tbuilder.Password = "password"; EntityConnectionStringBuilder ebuilder = new EntityConnectionStringBuilder(); // Setting up the connection for the Entity Framework ebuilder.Metadata = @"res://Example/Model1.csdl|res://Example/Model1.msl|res://Example/Model1.ssdl"); ebuilder.Provider = "Teradata.Client.Provider"; ebuilder.ProviderConnectionString = tbuilder.ToString(); EDMExample context = new EDMExample(ebuild.ToString()); var query = from o in context.Orders join od in OrderDetails on o.OrderID equals od.OrderID select new {o.OrderDate, od.ProductID, od.UnitPrice}; // Executing the Linq statement and retrieving the data of the properties foreach(var result in query) { Console.WriteLine("OrderDate = {0}, ProductID = {1}, UnitPrice = {2}", result.OrderDate, result.ProductID, result.UnitPrice); } } |
The following example uses Entity SQL to retrieve the same properties as the previous Linq to Entities example.
Entity Provider Example 2 |
Copy Code |
---|---|
public void EntityProviderExample2() { // Setting up the connection string to the data provider TdConnectionStringBuilder tbuilder = new TdConnectionStringBuilder(); tbuilder.Database = "Database"; tbuilder.DataSource = "Source"; tbuilder.UserId = "user"; tbuilder.Password = "password"; EntityConnectionStringBuilder ebuilder = new EntityConnectionStringBuilder(); // Setting up the connection for the Entity Framework ebuilder.Metadata = @"res://Example/Model1.csdl|res://Example/Model1.msl|res://Example/Model1.ssdl"); ebuilder.Provider = "Teradata.Client.Provider"; ebuilder.ProviderConnectionString = tbuilder.ToString(); EDMExample context = new EDMExample(ebuild.ToString()); String queryStr = "select o.OrderDate, od.ProductId, od.UnitPrice from Orders as o join OrderDetails as od on o.OrderID == od.OrderID"; ObjectQuery<DbDataRecord> query = context.CreateQuery<DbDataRecord>(queryStr); // Executing the Linq statement and retrieving the data of the properties foreach(var result in query) { // NOTE: To retrieve a DATE, GetDateTime must be called Console.WriteLine("OrderDate = {0}, ProductID = {1}, UnitPrice = {2}", result.GetDateTime(0), result.GetInt32(1), result.GetDecimal(2), result.UnitPrice); } } |
For details regarding the ADO.NET Entity Framework, please refer to MSDN.
Querying with LINQ to Entities