Introducing the Entity Framework
Welcome to this installment of the .NET Nuts & Bolts column. A few the recent .NET Nuts & Bolts columns have been about LINQ. Articles such as an Introduction to Language Integrated Query (LINQ), LINQ to XML, and LINQ to SQL have explained what LINQ is and how to use it with XML and SQL. This article introduces yet another flavor—LINQ to Entities, which is a part of the Entity Framework.
Introduction to the Entity Framework
The Entity Framework is a new part of ADO.NET that allows you to build your applications against conceptual data models. It provides a greater level of abstraction and supports code that is independent of any particular relational database. It provides an Entity Data Model (EDM) for defining data at the database and conceptual level and mapping between the two. It includes a nice set of tools that can be used to generate the EDM and corresponding objects that represent the database. This eliminates much of the required boilerplate data access code, and makes it a snap to create data-centric applications.
Generating the Entity Data Model
You'll find the simplicity of generating Entity Data Models a pleasant surprise. And it's even more pleasant when you discover how simple it is to keep the model synchronized with the database. The examples in this article use the Northwind sample database in SQL Server. First, create a new Console application to use in this example. Next, add an EDM object to the project (see Step 1, below). It is possible to use the EDM Generator command prompt utility, but given that I'm a fan of GUIs and simplicity, I prefer to use the integrated set of Entity Data Model tools released with Visual Studio 2008 Service Pack 1. Here are the steps:
Figure 1. Entity Data Model Wizard: Using the wizard, you can elect to generate a model from a database or create an empty model.
- Right-click the project in Solution Explorer, elect to add a new item, choose "ADO.NET Entity Data Model" as the new item type, and name it NorthwindModel.edmx. This initiates the Entity Data Model Wizard in Visual Studio, where you can generate the model from a database or create an empty model (see Figure 1).
- In the next wizard step, choose your data connection. For this example, I established a new connection to the Northwind database on my local SQL Server instance (see Figure 2). I tested the connection, verified that it was working, and then allowed it to save the connection setting in the app.config file.
- The final step in the wizard is to choose which database objects you want to include in your model (see Figure 3). For this example, I chose the entire Tables tree, and then clicked Finish to generate the model.
Figure 2. Connection Properties: Fill out this wizard page to establish a connection to the Northwind database.
Figure 3. Choose Database Objects: For this example, select the Tables branch of the tree, and then click Finish.
Completing the wizard resulted in Visual Studio displaying a design surface with a model similar to the one in Figure 4 (I rearranged some of the shapes so that the entire model fit on the screen).
Figure 4. Model Design Surface: You can drag the shapes around on the design surface to arrange the model to meet your needs.
The integrated wizard in Visual Studio 2008 makes the process straightforward. I have used other third-party components that follow the same process to establish a model that can connect to Oracle or some other database provider as well.