Saturday, July 07, 2007

Entity Framework June 2007 CTP Visual Basic Samples

As usual, the sample code included with the Entity Framework June 2007 CTP was written in C#. So I ported the basic elements of the C# EFJuneCTPSamples Web Site project to the VB EFJuneCTPwithVB ASP.NET Web Site (file system) project to determine if there were unexpected issues with VB 9.0. Updated 7/6/2007: Added detail text and a GridView page populated by an ObjectQuery from an Entity SQL statement. Note: John Papa's ADO.NET Entity Framework Overview in MSDN Magazine's July 2007 Data Points column provides a summary of EF features of the Orcas Beta 1 version. LINQ to Entities ObjectQueries The LINQGrid.aspx page executes a relatively simple LINQ to Entities ObjectQuery query against the C# sample's default mapping of an extended version of the Northwind Sample database. ObjectQuery objects implement IOrderedQueryable, IQueryable, IEnumerable(Of T), IQueryProvider, IOrderedQueryable, IQueryable, and IEnumerable, so you can bind them to GridView and other databound Web controls:

Click images to display full-size versions

Notice that the GridView maps only String data to columns automatically but the CustomerID foreign-key value is missing. If you want to display foreign key values, you must obtain them from the primary key of the related entities. (There is a request pending to provide read-only access to foreign-key values so associated entities don't need to be loaded to obtain key values.) You can bind GridView columns to OrderDate, RequiredDate, ShippedDate and Freight properties manually. The Country value isn't passed as a parameter to keep the query expression simple.

ObjectQuery also supports the ToBindingList method, which (theoretically) enables two-way (read/write) data binding with DataGridView controls.

The LINQText.aspx page has a more complex query with a specific projection and uses the Take(10) partition operator to display the last 10 orders from Mexico with a VB clone of the ObjectDumper utility.

The preceding page displays the same fields as those of the WinForms test harness for LINQ to SQL performance tests in my earlier posts here and here.

References and Namespaces for LINQ to Entities Queries

The sample Web Site project adds references to System.Data.Entity.dll and System.Data.DataSetExtensions.dll in the \Program Files\Reference Assemblies\Microsoft\Framework\v3.5 folder. System.Data.Entity.dll contains all EF-specific namespaces.

Only the default Imports System.Linq directive from System.Core.dll is needed for ordinary queries.

Using Entity SQL Statements with ObjectQuery Objects

The following page demonstrates the current state of Entity SQL (eSQL) syntax for ObjectQuery. Use of the related Customer entity's Country property as the restriction is contrived because ShipCountry could have provided this information (although it might differ from the Customer.Country value.)

ObjectQuery objects returned by eSQL queries that specify projections of properties with SELECT (instead of SELECT VALUE) return MaterializedDataRecords, not a type that implements IEnumerable. Dealing with DbDataRecord types isn't fun by any stretch of the imagination. Stick with LINQ for Entities queries when you want to return specified property (field) values.

Note: For a short course on Entity SQL, read the ADO.NET Team's EntitySQL post of May 30, 2007 by Mark Shields and Fabio Valbuena. Mike Dodaro's Entity Data Model 101: Part 1 (January 30, 2007) and Entity Data Model 101 - Part 2 (February 12, 2007) explain the EDM.

The following page substitutes a simplified Entity SQL query for the LINQ to Entities expression to verify that the GridView control responds to the ObjectQuery identically.

The GridView is missing the same fields as the first example.

Namespaces for Entity SQL ObjectQuery Operations

In addition to the default Imports System.Linq directive used with LINQ to Entities queries, add Imports System.Data.EntityClient and Imports System.Data.Objects directives.

2 comments:

Anonymous said...

I have a question:

I am creating an eSql dynamically and returning DbDataRecord. Is it possible to bind this DbDataRecord to a WPF datagrid in any way ?

Anonymous said...

Do I know you? I am trying to achieve exactly the same thing. Creating eSql dynamically and returning List of DbDataRecords. If i bind it directly to a dataGrid, it gives me one column named FieldCount.
I haven't found a way to get the columns. Any solution?