Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 20th, 2011, 08:07 PM
Registered User
Points: 35, Level: 1
Points: 35, Level: 1 Points: 35, Level: 1 Points: 35, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default chapter 14 Try It Out pg 505 step #4

My question concerns the Where clause: Where="it.PhotoAlbum.Id = @photoAlbumId" (see code below)

Why is it that the Where clause uses the property PhotoAlbum from the EntitySetName Pictures to get to Id field of table PhotoAlbum i.e. it.PhotoAlbum.Id. Why doesn't it use it.PhotoAlbumId directly, from table Pictures, in setting up the Where clause? What am I missing here?

Thanks ahead of time for any help offered.

Code:

</asp:ListView>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=PlanetWroxEntities"
DefaultContainerName="PlanetWroxEntities" EnableDelete="True" EnableFlattening="False"
EnableInsert="True" EntitySetName="Pictures" Where="it.PhotoAlbum.Id = @photoAlbumId">
<WhereParameters>
<asp:QueryStringParameter Name="PhotoAlbumId" QueryStringField="PhotoAlbumId" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
</asp:Content>
  #2 (permalink)  
Old April 21st, 2011, 02:52 AM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

It's an Object Orientation versus a relational database approach that made me prefer one over the other:

I think this feels much more natural:

Code:
it.PhotoAlbum.Id
than this:

Code:
it.PhotoAlbumId
if you look at it from an Object Orientation perspective. In an OO world, a Picture has a PhotoAlbum property that refers to the album it belongs to. The fact it needs stuff like Ids to create the relation in the database is a side issue ;-), let alone the fact it needs a foreign key property called PhotoAlbumId which is merely there for optimization.

In earlier versions of EF, a Picture wouldn't even have a PhotoAlbumId property and you had to do some weird stuff to associate them (not query them as in your example). In EF4, the foreign property was added to make assigning one object to another easier. E.g.:

Code:
myPicture.PhotoAlbumId = 3;
rather than:

Code:
myPicture.PhotoAlbum = context.Albums.Where(p => p.Id == 3).First();
The inclusion of the foreign key property is done to optimize performance and make updates easier.

But, both solutions work fine. So it's perfectly OK to use PhotoAlbumId if that feels more natural to you.

Hope this helps.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!

Last edited by Imar; April 21st, 2011 at 02:56 AM..
  #3 (permalink)  
Old April 21st, 2011, 10:35 AM
Registered User
Points: 35, Level: 1
Points: 35, Level: 1 Points: 35, Level: 1 Points: 35, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Default chapter 14 Try It Out pg 505 step #4: Where clause

Thanks, Imar. Your explanation makes perfect sense. By the way, I am getting a lot out of your book. I highly recommended it to any beginner like me.
  #4 (permalink)  
Old April 21st, 2011, 02:28 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You're welcome.

Quote:
By the way, I am getting a lot out of your book. I highly recommended it to any beginner like me.
That's good to hear. Tell all your friends, colleagues and Amazon.com.... ;-)

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 13 Page 470 Step 14 bagellady BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 8 September 17th, 2010 12:36 PM
Chapter 5 | Pg.158 - Step 2 jwriddle BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 30th, 2009 02:03 AM
Chapter 4|Pg.120 - Step 7 jwriddle BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 April 28th, 2009 04:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.