Mapping Using the O/R Designer

The first step in creating a LINQ to SQL application is typically the construction of an object model that is based on a given database definition. This is the exact function of the O/R designer. It enables you to select a database and generate an object model that maps to the database’s structure. Table 13.3 shows how the database components are mapped to object components.

Image

TABLE 13.3 Default Database to Object Mappings

Adding Database Entities

The O/R designer is the design surface for project items known as LINQ to SQL Classes, so the first step in using the designer is to add a new LINQ to SQL Classes project item to a project. Figure 13.51 shows where this project item lives in the Add New Item dialog box.

Image

FIGURE 13.51 Adding a LINQ to SQL Classes item to an existing project.

After you’ve selected the LINQ to SQL Classes item and added it to the project, the O/R designer immediately launches.

There isn’t much to see yet because we haven’t selected which database entities we want to represent within our object model. This involves the use of the second primary tool for performing the O/R mapping: Server Explorer.

By selecting a valid data source in Server Explorer, we can simply drag and drop a table onto the left side (the “data class” side) of the O/R designer (see Figure 13.52).

Image

FIGURE 13.52 The AdventureWorks PurchaseOrderHeader table added to the O/R design surface.

Although nothing obvious happens after the table is dragged onto the data class pane (beyond having its visual representation in the designer), in reality potentially thousands of lines of code have been automatically generated to implement a class structure that mimics the table structure. In addition, all the attribute-based wiring has been implemented so that the LINQ engine can understand and process updates between the class object and the table’s rows and columns.

This exact process is used to create methods within our object model. We can, for instance, drag a stored procedure onto the right pane of the designer (the “methods” pane) to map a method within our object model to the stored procedure (see Figure 13.53).

Image

FIGURE 13.53 A stored procedure mapped as a method.

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

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