FREE BOOK

Chapter 8 : Reading Objects with LINQ to SQL

Posted by Addison Wesley Free Book | LINQ July 28, 2009
It uses the mapping of classes to tables to translate LINQ queries to SQL commands and then materializes objects from the rows returned. The objects can be related to each other in a graph of objects that is managed by LINQ to SQL on your behalf.

Mapping Different Types of Relationships
 
The -E s example has a one-to-many relationship. There may be many orders for a given customer, but no more than one customer exists  for a given E . LINQ to SQL also supports the relatively less common one-to-one relationship, such as a Spouse for an Employee.
 
In a third kind of relationship called many-to-many, such as E -) " , an order may cover multiple products, and a product is included in multiple orders. In relational databases, such a relationship typically is normalized through a "link table" or a table in the middle. The Order Details table in the Northwind database relates orders and products by including foreign keys from both the related tables. However, such relationships often have some important additional data in the link table. For example, Order Details specifies the quantity, price, and so on. Hence, in the interest of simplicity, LINQ to SQL supports the many-to-many relationship as a pair of one-to-many relationships-E -E A? and ) " -E A? . This allows the properties on the relationship entity to be naturally modeled, such as E A? ) " . However, regardless of the presence of the additional properties, the class mapped to the link table is required in LINQ to SQL. Even if you do not intend to map E A? ) " , E A? class cannot be skipped with just E ) " and ) " E collections.
 
As previously shown, the mapping for relationships can be written by hand. However, the designer makes relationships even simpler. When a  pair of related tables are dragged and dropped onto the designer surface, the designer infers the relationship between them based on foreign keys in the database. It generates the necessary members and mapping automatically. The relationship between the and E classes is represented  by an arrow between the classes, as shown in Figure 8.3.
 
 
 
Figure 8.3 The relationship between entity classes.
 
The relationship represented by the arrow can be selected and edited through an association editor or the property grid. The property in the grid indicates whether the relationship is one-to-many or oneto- one. The direction of the arrow represents the parent-to-child direction. Following the Entity-Relationship model used for database modeling, the entity containing the foreign key is called the child, and the entity identified by the foreign key is called the parent. In the -E relationship shown in the figure, the arrow is from the parent, , to the child, E . By default, the designer creates a relationship property on each end of a relationship. The property in a child entity referencing the parent entity is required. The reverse property in the parent entity identifying the children or child is optional and can be removed. Relationships allow you to think about your object model as a connected graph. LINQ to SQL operates on such a connected graph while generating SQL to operate on underlying tables joined using keys. Using the dot to refer to related entities in queries is one direct benefit of the object graph model. Another benefit is the ability to load the target of a relationship. Next we'll look at the options available for loading related entities.

Total Pages : 9 34567

comments