Creating a Conceptual Model with the Model Wizard
The database's physical model is how the database engine thinks of the data files. The logical model is how the database architect thinks of the data in terms of keys, constraints, normalization, and rows and columns. The conceptual model is how the programmer would like to think of the data: customers, addresses, sales orders, products, or whatever the data in the programmer's problem space represents.
The Entity Framework wizard in Visual Studio lets you choose the relationships that interest you in terms of views, tables, and procedures. It creates code-generated entity classes representing how you need to think of the data to solve the problem. Keys, relationships, and constraints are handled by the generated code, so you get to focus on what the data and its relationships mean and how they're used; for example, managing customer orders or vehicle registrations, or parts inventories and suppliers. You can create as many entities as you need to satisfy your problem space.
To create an entity data model (and generate the code), you'll need Visual Studio 2008 Service Pack 1, and you'll need to add an ADO.NET Entity Data Model item from the Add New Item dialog box. Selecting the ADO.NET Entity Data Model item starts the wizard.
For example, to generate a view of customers, orders, and order details from the Northwind database, run the wizard and follow these steps:
- Add an ADO.NET Entity Data Model item to any project. (The sample code is a console application.)
- In the first step of the wizard, select Generate from Database and click Next.
- In the Choose Your Data Connection step, select the Northwind database and provide a meaningful name for the connection, clicking Next when you're finished.
- In the Choose Your Database Objects step, expand the Tables objects and select Customers, Orders, and Order Details.
- After selecting your objects, click Finish.
Clicking Finish starts the code generator, which will create an entity data model, which includes XML for the visual designer (see Figure 1) and some strongly typed code. The code will use classes inherited from EntityObject and relationships inferred from keys. For example, the Customers entity will contain an Orders collection defined as EntityCollection<Orders>. Customers and Orders can be accessed through these objects without writing any database plumbing.
Figure 1 An entity data model in the designer view.
When you query or update the entities, Entity SQL—which is based loosely on SQL—will do the SQL work. The difference between SQL and Entity SQL (eSQL) is that eSQL is written with an emphasis on entities, rather than traditional joins and key columns.