February 26, 2021
Hot Topics:

LINQ to Entities Preview

  • By Paul Kimmel
  • Send Email »
  • More Articles »

After you click Finish, your friend the CodeDOM will be employed to generate strongly typed entity classes, an Entity Data Model XML file, which is expressed visually in a data model designer (see Figure 5). These entity classes inherit from System.Data.Objects.DataClasses.EntityObject. The AdventureWorksEntities inherits from System.Data.Objects.ObjectContext.

Click here for a larger image.

Figure 5: The visual designer that represents entities that you can query with LINQ to Entities (or alternatively eSQL; I recommend you use LINQ to Entities).

The ObjectContext is similar in function to the DataContext from LINQ to SQL, and the EntityObject is similar in usage to the entity classes in LINQ to SQL tagged with the TableAttribute. ObjectContexts represents your database connection and EntityObjects represent your table entities.

There are two things to think about before you proceed. The first is that generated classes are partial classes, so add any custom code to a separate file containing the other part of the partial class. The second thing to keep in mind is that one EDMX file should not contain your entire database. Only capture fairly singular facets in a single EDMX file—for example, in the example you can explore employee shift histories—and create additional sub-models for additional facets. Don't worry about overlapping generated code. It doesn't matter.

The important thing to remember here is that you didn't write any of this code yet. The cost is almost zero (and disk storage is cheap). Now, you are ready to start querying with LINQ.

Ready to Query

To this point, you haven't had to write any code. The preceding steps and text take longer to read (and much longer to write) then the whole process takes. You can create a project and add an ADO.NET Entity Model in seconds. And, these models support reading and writing.

As mentioned, you can use Entity SQL to interact with Entity Models but Entity SQL (or eSQL) may have been a clever invention that preceded LINQ to Entities slightly or was a case of someone's unnecessary cleverness. If you want to weight how you spend your time, I would suggest you focus your efforts on LINQ.

Because LINQ to Entities is similar in some ways to LINQ to SQL, you need to perform some similar kinds of steps. First, create the entity classes defined by the wizard. This will provide you access to the underlying database. Second, write your LINQ queries, interact with or update the data, and save the changes. At least in, demo scenarios, it's that easy (see Listing 1).

Listing 1: LINQ to Entities is as easy as LINQ to Objects because you are programming against objects, entity objects.

Imports LinqToEntities_VB.AdventureWorksModel
Imports System.Linq
Imports System.Text.RegularExpressions

Module Module1

   Sub Main()
      Dim entities As AdventureWorksEntities =
         New AdventureWorksEntities

      ' Select
      Dim employees = From emp In entities.Employee _
                      Select New With { _
                         .EmployeeID = emp.EmployeeID, _
                         .FirstName  = emp.Contact.FirstName, _
                         .LastName   = emp.Contact.LastName, _
                         .Salutation = emp.Contact.Title _

      For Each employee In employees
         Console.WriteLine("{0} {1} {2}", employee.Salutation, _
            employee.FirstName, employee.LastName)

      ' Update
      Dim mispelled = (From emp In entities.Employee _
                       Where emp.EmployeeID = 1 _
                       Select emp.Contact).First()

      mispelled.LastName = "Gelbert"

   End Sub

End Module

Page 3 of 4

This article was originally published on June 25, 2008

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date