Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5 > BOOK: Beginning ASP.NET 4.5 : in C# and VB
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4.5 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4.5: in C# and VB by Imar Spaanjaars; ISBN: 978-1-118-31180-6
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4.5 : 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
 
Old May 18th, 2014, 11:12 PM
Authorized User
Points: 276, Level: 5
Points: 276, Level: 5 Points: 276, Level: 5 Points: 276, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 39
Thanks: 4
Thanked 1 Time in 1 Post
Default Chapter 15, Try It Out Hand-Coding Data Access Pages

Hello,

In the subsequent How It Works for this exercise (pg 586), the text reads
"For the genre, the code directly assigns the GenreId property instead of querying a complete Genre instance and assigning it to the Genre property of the Review instance. This works because the model has support for foreign key columns, as you learned in the preceding chapter."
I contend that it's actually because the Genre's ID is selected in the DropDownList which is bound to the EntityDataSource, thus providing a valid ID.

If I am misreading this please explain further. Thank you in advance for the explanation.

Thanks,
WebDev123
 
Old May 19th, 2014, 08:20 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

Well, yes and no.

The point I am making is that you can assign an arbitrary number to the GenreId instead of querying a genre instance. E.g. without the Foreign Key option, you couldn't do this:

Code:
myReview.GenreId = 123;
but instead you had to do this:

Code:
myReview.Genre = _dataContext.Genres.First(x => x.Id == 123);
which is harder to read and means an additional call to the database.

What you're saying is true too. The Genre ID comes from the dropdown list so you can use it. But you can use the incoming ID directly instead of querying the whole genre because of the Foreign Key option in the EF designer.

Hope this helps,

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!
 
Old May 27th, 2014, 11:57 PM
Authorized User
Points: 276, Level: 5
Points: 276, Level: 5 Points: 276, Level: 5 Points: 276, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 39
Thanks: 4
Thanked 1 Time in 1 Post
Default Reply

Okay, I reviewed Chapter 14 again. During the first Try It Out (pg. 502) we ensured the option for the Entity Model was set to "include foreign key columns". This allows us to perform the extension method of .include() at the end of a query (illustrated at the bottom of pg. 505 and 512).

In the How It Works section (bottom of pg. 515) you describe it. Then on page 523 you set up creating a query string. On the bottom of page 535 you reinforce the concept.

However, in the Try It Out Hand-Coding Data Access Pages example I don't see why you would need that. Let me explain my understanding:

When the AddEditReviewHandCoded page is loaded, it determines whether or not there's a Reivew Id passed in. If there isn't one, you can create a new Review and pick the Genre (and GenreId) from a dropdownlist. Seems simple enough.

If there IS an Id passed, then you query the database for that Review, and pre-populate the controls. In this case, that Review already has a value for GenreId in its database row (whether or not the Entity Model was set to "include foreign key columns" or that we added the .include() method). So when I query it and pull it up, why wouldn't I just pre-populate the dropdownlist with THAT value? Why would I have to query a Genre instance or any foreign key values?


Thanks for your time,
WebDev123
 
Old May 28th, 2014, 06:11 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

Quote:
e Entity Model was set to "include foreign key columns". This allows us to perform the extension method of .include() at the end of a query
These two are unrelated.

In case this clears up the confusion: it's the GenreId property that gets added with that option, not the Genre property itself. The Include method has always been available in EF and allows you to eager load related objects like the Genre instance for a Review.

Quote:
If there isn't one, you can create a new Review and pick the Genre (and GenreId) from a dropdownlist. Seems simple enough.
Indeed. Because of the Foreign Key options. That allows you to do something like this:

Code:
var review = new Review();
review.GenreId = Convert.ToInt32(GenreList.SelectedValue);
Ths simply assigns the GenreId by retrieving it from the selected value of the drop down list.

Without the Foreign Key option, it would have looked like this instead:

Code:
var review = new Review();
review.Genre = (from g in db.Genres
                     where Id == Convert.ToInt32(GenreList.SelectedValue)
                     select g).FirstOrDefault() ;
This causes an extra round trip to the database to fetch the entire genre. A waste, as all we're really interested in is the ID of the genre which we already have.

Quote:
In this case, that Review already has a value for GenreId in its database row (whether or not the Entity Model was set to "include foreign key columns" or that we added the .include() method).
That's not entirely correct. The *database* would have the GenreId column, but the Review class would not. All it would have is a Genre property of type Genre and it would be null if you didn't explicitly load it along with the Review by using Include() or lazy loading.
Quote:
So when I query it and pull it up, why wouldn't I just pre-populate the dropdownlist with THAT value? Why would I have to query a Genre instance or any foreign key values?
You can query the Review, and you would either get null for the Genre, or you could call Include and prepopulate the Genre proeprty so you can access its ID. However, this would be only be useful initially to preselect the item.

If you change the Review in the UI and choose a new Genre from the drop down, all that's submitted is the GenreId. Then at the server side, you would need to find a way to assign that GenreId to the Review instance again (whether it's new or not). Without the Foreign Key option you would need to manually query the Genre from the database. With this option, all need to do is assign the GenreId without an additional roundtrip.

Does this help in understanding the process?

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!
 
Old May 28th, 2014, 09:18 PM
Authorized User
Points: 276, Level: 5
Points: 276, Level: 5 Points: 276, Level: 5 Points: 276, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 39
Thanks: 4
Thanked 1 Time in 1 Post
Default

QUOTE]it's the GenreId property that gets added with that option, not the Genre property itself.[[/QUOTE]
- So you're differentiating between the actual property that gets added and the value of that property.

Quote:
That's not entirely correct. The *database* would have the GenreId column, but the Review class would not. All it would have is a Genre property of type Genre and it would be null if you didn't explicitly load it along with the Review by using Include() or lazy loading.
- Okay, that's a nuance that I wasn't aware of from not using it enough. I thought it would include a Genre property value, not just a Type.

Quote:
You can query the Review, and you would either get null for the Genre, or you could call Include and prepopulate the Genre proeprty so you can access its ID. However, this would be only be useful initially to preselect the item.
- Okay, we're on the same page now.

Quote:
Does this help in understanding the process?
- Yes it helps with understanding the EF concepts, thanks for the effort; I'm sure others will benefit from this as well!
 
Old May 28th, 2014, 09:24 PM
Authorized User
Points: 276, Level: 5
Points: 276, Level: 5 Points: 276, Level: 5 Points: 276, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 39
Thanks: 4
Thanked 1 Time in 1 Post
Default

These EF calls bring another question to mind with your presentation examples. I have heard from "advanced" developers that you should use a "data layer" - a class or collection of classes - to manage all the database queries and interaction. But here, all of your examples are done in event handlers. Can you comment on that?
 
Old May 28th, 2014, 10:59 PM
Authorized User
Points: 276, Level: 5
Points: 276, Level: 5 Points: 276, Level: 5 Points: 276, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2012
Posts: 39
Thanks: 4
Thanked 1 Time in 1 Post
Default

Yet another question in this regard. In this chapter, I really like the flexibility of adding my own data controls instead of relying on ones in the toolbox. I've read that tables should not be used as formatting tools. When we added the controls in this exercise, we used a table for alignment purposes. Which is the preferred technique? It seems like this is most useful and it simplifies things. Can you comment on this?

Thank you,
WebDev123
 
Old May 29th, 2014, 07:49 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,

Quote:
So you're differentiating between the actual property that gets added and the value of that property.
Nope, that's not what I meant. I am talking about the different between a GenreId property of type int and a Genre property of type Genre. The former just holds the ID of the genre in the database, while the latter holds the full Genre instance, including its ID and Name. In older versions of EF you would only have access to the full Genre, not to its ID directly.

Quote:
Okay, that's a nuance that I wasn't aware of from not using it enough. I thought it would include a Genre property value, not just a Type.
Not sure what you mean with "a Type". Each property is always of a certain .NET type. GenreId would be an int, while Genre would be of type Genre.

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!
 
Old May 29th, 2014, 07: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

Quote:
I have heard from "advanced" developers that you should use a "data layer" - a class or collection of classes - to manage all the database queries and interaction.
You may want to check out my article series on N-Layer design for ASP.NET: http://imar.spaanjaars.com/573/aspne...duction-part-1 It deals with this, and many other advanced concepts in great detail.

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!
 
Old May 29th, 2014, 07:57 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

Quote:
In this chapter, I really like the flexibility of adding my own data controls instead of relying on ones in the toolbox.
Not sure what you mean with this. Can you elaborate?

Quote:
I've read that tables should not be used as formatting tools.
When we added the controls in this exercise, we used a table for alignment purposes. Which is the preferred technique? It seems like this is most useful and it simplifies things.
Yes, I used tables as it's quicker in an exercise like this one. The main purpose of the exercise is handcoding data access with EF, so I didn't want to spend 3 pages on setting up a div based form using CSS instead of tables.

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!




Similar Threads
Thread Thread Starter Forum Replies Last Post
Hand coding data access code Wilfredo Rosado BOOK: Beginning ASP.NET 4 : in C# and VB 10 January 10th, 2014 04:29 PM
Using entities with hand-coding ASP4student BOOK: Beginning ASP.NET 4 : in C# and VB 2 February 5th, 2013 07:58 AM
ch. 15 - Hand Coding Data Acess Code Tulsi BOOK: Beginning ASP.NET 4 : in C# and VB 6 January 11th, 2013 03:45 AM
P468 Hand-Coded Data Access Page Powerplanner BOOK: Beginning ASP.NET 4 : in C# and VB 1 December 4th, 2011 06:18 AM
Hand Coding VS Data Wizards Bulldog BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 May 22nd, 2007 04:21 AM





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