Creating the Home Page for a Simple Storefront with LINQ - Preparing to Build the Product Page
(Page 4 of 4 )
Now we can create Product.aspx, the page where the details of an individual product can be viewed. Go ahead and create Product.aspx. Make sure you set MasterPage.master as its master page.
Before we do anything, recall that the description of a product is not in the Product table. Rather, descriptions are associated with models through the ProductModelProductDescription table. So, in order to get the description of an item, we need to first determine a product's model. It's a long trail, though. We need to go from Product to ProductModel to ProductModelProductDescription to ProductDescription. We need to add these tables to AdventureWorks.dbml before we continue with the Product.aspx page:

Now, pay attention to the direction of the arrows, which represent associations. Notice how the arrow between ProductModel and ProductModelProductDescription is pointed away from Product, while every other arrow is pointed toward Product. This is because ProductModel is set as the parent class, and ProductModelProductDescription is set as the child class. Basically, the two terms involve how each table handles the field around which the association is built. With ProductModel and Product, for example, the association is built around ProductModelID. In the ProductModel table, this field is the primary key, and in Product, it's just a normal field. So, ProductModel is the parent class here. However, with ProductModelProductDescription and ProductModel, both tables have ProductModelID as a primary key, so either table can be the parent class.
For our purposes, the association is backward, though. The association suggests that more than one entry in the ProductModelProductDescription table (the child table) can be matched with a given ProductModelID (part of the parent table), just as more than one entry in the Product table can be matched with a given ProductModelID (part of the parent table). It's entirely possible for us to build our application with this configuration, but then to access the ProductDescription for a given Product (p), we have to use First, which is ugly and troublesome:
p.ProductModel.ProductModelProductDescriptions.First().ProductDescription.Description
Fixing this is easy. Click the arrow and delete it to delete the association. Then, right click the Designer and add a new association. Set ProductModelProductDescription as the parent and ProductModel as the child. Then, select the ProductModelID property for both of them:

Now all of the arrows face the same direction:

This makes more sense: while every ProductDescription can be matched with multiple Product entries, every Product can only be matched with one ProductDescription. Now we can drop the call to First when accessing a Product's description:
p.ProductModel.ProductModelProductDescriptions.ProductDescription.
Description
This is a lot more logical.
We're almost ready to begin work on Product.aspx. Just add one final style definition to StyleSheet.css, and we'll be ready to start work:
.itemBoxLarge
{
background-color: #F0FFFF; /* Azure */
border: solid 1px Orange;
margin: 5px auto 0px auto;
padding: 5px;
text-align: left;
width: 70%;
}
Don't forget to check back next week for the third part of this three-part series!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |