Wrox Programmer Forums
|
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
 
Old December 3rd, 2011, 03:03 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile Single Review assigned to multiple Genres

Greetings!!

Although book shows that we can assign a single Review to single Genre only. But is it possible to assign single Review to multiple Genres? If yes, then where the code should be changed?

Thanks...
 
Old December 4th, 2011, 06:13 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You would create a new table with the following two columns:

ReviewId int
GenreId int

Then you need to link the two related tables (Review and Genre) to their respective columns in this new table. If you now update or recreate the EDMX you get a many to many relationship, so you can do stuff like this:

myReview.Genres.Add(genre1);
myReview.Genres.Add(genre2);

Where the genre variables point to valid instances of the Genre class.

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 December 6th, 2011, 01:39 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Thanks Imar, I will try this and update here...
 
Old December 7th, 2011, 01:35 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

I am just gonna implement it, but one thing I would ask that isn't there any other way to implement many to many or one to many relationship instead of creating and maintaining separate table?

Thanks
 
Old December 7th, 2011, 02:32 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

One to many: yes, there is, as is shown with the Reviews and Genre (and Album and Picture tables). For many to many you need a separate table to link multiple records to each other.

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 January 15th, 2012, 01:33 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Back to topic,
I have slightly changed your hand code example on page no 555.
while inserting many Genries for single Review in third table, there is no any need to take much care but I am stuck with updating those many Genre Ids for a single Review in third table.

I want that Genre Ids should be updated only when they are actually changed by user.

I used checkboxlist instead of ddl and bound it similarly as you did.
Code behind is

Code:
protected void SaveButton_Click(object sender, EventArgs e)
{
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
Review myReview;
if (_id == -1) // Insert new item
{
myReview = new Review();
myReview.CreateDateTime = DateTime.Now;
myReview.UpdateDateTime = myReview.CreateDateTime;
myEntities.AddToReviews(myReview);
}
else // update existing item
{
myReview = (from r in myEntities.Reviews
                         where r.Id == _id
select r).Single();
myReview.UpdateDateTime = DateTime.Now;
}
myReview.Title = TitleText.Text;
... 
...  
myEntities.SaveChanges();

foreach (ListItem li in GenreList.Items)
            {
                if (li.Selected)
                {
                    myGenRev = new GenresReview();
                    myGenRev.GenreId = Convert.ToInt32(li.Value);
                    myCatRel.ReviewId = myReview.Id;
                    myEntities.AddToGenresReview(myGenRev);
                    myEntities.SaveChanges();
                }
            }

}
So,
1.) how I enhance the foreach loop so that it updates the record in third table (GenresReview) when they have been changed by user.
2.) I also know that it will require the deletion of existing records for that single Review as user may choose less Genres than the previous.

Kindly suggest the solution keeping the above 2 point in mind.
 
Old January 15th, 2012, 04:22 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If your relation object contains no other data (e.g. you just store the two foreign keys), here's what would work:

Call Load on the collection so you get all relation records from the database
Call Clear
Add the selected items by instantiating new relation objects.

You may want to consider getting the Entity Framework book by Julia Lerman; it shows you how to do this, and much more, with EF.

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 January 16th, 2012, 02:25 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Thanks Imar for points,
but can you please give small code snippets of all the three points you suggested.
According second point will this clear only those records which have actually been changed by user?

buying a book for single topic does not seems to be in my budget.

Thanks...
 
Old January 16th, 2012, 03:17 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Load and Clear are methods on your collection that you can call directly so you already have examples. And you already have the code for the third item.

This is a bit too off-topic for my book to discuss in full detail here. Google has lots of examples on this if you need more information.

If you're working with EF, that book is money well-spent.

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 January 16th, 2012, 05:40 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

HI Imar,
thanks I will give it try, but last question (which was more important)
Would it update only those records which have been really changed by user???





Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple views of single table jatatman ASP.NET 3.5 Basics 1 June 2nd, 2008 01:34 AM
Representing multiple items with a single value? Donny Bahama Classic ASP Professional 1 January 16th, 2006 09:32 PM
Multiple Projects under Single Solution gadhiav ASP.NET 1.0 and 1.1 Professional 2 October 20th, 2005 04:26 AM
multiple selects -single where clause collie SQL Server 2000 3 January 21st, 2005 01:27 AM
Single or Multiple application. Gibs_poovath General .NET 7 May 25th, 2004 07:54 AM





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