Chapter 7: Working with Data

201

To add entities to the LINQ to SQL Designer, open Server Explorer, select a database, and open the Tables folder. Then drag and drop the Customer and Order tables from Server Explorer to the left surface of the LINQ to SQL Designer. You can see the Customer and Order classes in Figure 7-11, along with properties corresponding to the fields of each table in the database.

The line between Customer and Order is called an association. As you might guess from reading the previous discussion on class relationships, the association defines the relationship between two classes. Although a relationship between tables is constrained by a foreign key in a child that refers to the primary key of that child’s parent, an association is the reverse direction; it is a property of a parent class that refers to all of the children of that class. When coding, you can use this association to navigate between parent and child objects.

NOTE

Features, such as the difference between foreign key relationships in relational databases and associations in object-oriented code, are often referred to as an impedance mismatch, a term taken from electrical engineering, between data and objects. LINQ is designed to reduce the impedance mismatch by allowing you to work with data from an object-oriented point of view, rather than doing all of the low-level work yourself such as copying data records into data transfer objects, DTOs, that you design and create.

On the right pane of Figure 7-11, you can see a GetCustomers method, which allows you to call the GetCustomers stored procedure. You can put stored procedures, such as GetCustomers, onto the design surface by opening the Stored Procedures folder of the database in Server Explorer and dragging and dropping that stored procedure onto the right pane of the LINQ to SQL Designer.

If your database has views and functions, you can add them the same way as you did for classes and functions previously. Before showing you how to use these new classes and views, I’ll show a little more about what you can do with the LINQ to SQL Designer.

Working with the LINQ to SQL Designer

While the most important part of the LINQ to SQL Designer is being able to add classes and methods, you should also know about some if its features such as the Methods pane hiding, zooming, and auto-layout. You’ll see these options through the design surface context menu (right-click).

Most of the time working with the Designer is with classes, and you want as much screen real estate as possible. You can achieve this goal by hiding the Methods pane. Just right-click the design surface and select Hide Methods Pane. Similarly, select Show Methods Pane to make the Methods pane reappear.

Page 224
Image 224
Microsoft 9GD00001 manual Working with the Linq to SQL Designer, 201