22.4 LINQ to Entities and the ADO.NET Entity Framework

When using the ADO.NET Entity Framework, you interact with the database via classes that the IDE generates from the database schema. You’ll initiate this process by adding a new ADO.NET Entity Data Model to your project (as you’ll see in Section 22.5.1).

Classes Generated in the Entity Data Model

For the Authors and Titles tables in the Books database, the IDE creates two classes each in the data model:

  • The first class represents a row of the table and contains properties for each column in the table. Objects of this class—called row objects—store the data from individual rows of the table. The IDE uses the singular version of a table’s plural name as the row class’s name. For the Books database’s Authors table, the row class’s name is Author, and for the Titles table, it’s Title.

  • The second class represents the table itself. An object of this class stores a collection of row objects that correspond to all of the rows in the table. The table classes for the Books database are named Authors and Titles.

Once generated, the entity data model classes have full IntelliSense support in the IDE. Section 22.7 demonstrates queries that use the relationships among the Books database’s tables to join data.

Relationships Between Tables in the Entity Data Model

You’ll notice that we did not mention the Books database’s AuthorISBN table. Recall that this table links

  • each author in the Authors table to that author’s books in the Titles table, and

  • each book in the Titles table to the book’s authors in the Authors table.

The entity data model’s generated classes include the relationships between tables. For example, the Author row class contains a navigation property named Titles which provides access to the Title objects representing all the books written by that author. The IDE automatically adds the “s” to “Title” to indicate that this property represents a collection of Title objects. Similarly, the Title row class contains a navigation property named Authors, which provides access to the Author objects representing a given book’s authors.

DbContext Class

A DbContext (namespace System.Data.Entity) manages the data flow between the program and the database. When the IDE generates the entity data model’s row and table classes, it also creates a derived class of DbContext that’s specific to the database being manipulated. For the Books database, this derived class has properties for the Authors and Titles tables. As you’ll see, these can be used as data sources for manipulating data in LINQ queries and in GUIs. Any changes made to the data managed by the DbContext can be saved back to the database using the DbContext’s SaveChanges method.

IQueryable<T> Interface

LINQ to Entities works through interface IQueryable<T>, which inherits from interface IEnumerable<T> introduced in Chapter 9. When a LINQ to Entities query on an IQueryable<T> object executes against the database, the results are loaded into objects of the corresponding entity data model classes for convenient access in your code.

Using Extension Methods to Manipulate IQueryable<T> Objects

Recall that extension methods add functionality to an existing class without modifying the class’s source code. In Chapter 9, we introduced several LINQ extension methods, including First, Any, Count, Distinct, ToArray and ToList. These methods, which are defined as static methods of class Enumerable (namespace System.Linq), can be applied to any object that implements the IEnumerable<T> interface, such as arrays, collections and the results of LINQ to Objects queries.

In this chapter, we use a combination of the LINQ query syntax and LINQ extension methods to manipulate database contents. The extension methods we use are defined as static methods of class Queryable (namespace System.Linq) and can be applied to any object that implements the IQueryable<T> interface—these include various entity data model objects and the results of LINQ to Entities queries.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset