Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 21st, 2010, 02:12 AM
Registered User
 
Join Date: Sep 2010
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Difficulty in DinnerRepository class

Hi,

I created private NerdDinnerEntities object 'entities'. There are two model classes in Entity i.e. Dinner and RSVPs. While implementing Insert/Delete method in DinnerRepository Class i stuck at entities.Dinner. because there is know Addobject() available neither the DeleteObject().

Help me out.

Thanks...
  #2 (permalink)  
Old September 23rd, 2010, 09:48 AM
Authorized User
 
Join Date: Sep 2010
Posts: 13
Thanks: 1
Thanked 1 Time in 1 Post
Default I'm listening....

as I have the same problem :-(
  #3 (permalink)  
Old September 24th, 2010, 06:15 AM
Authorized User
 
Join Date: Sep 2010
Posts: 13
Thanks: 1
Thanked 1 Time in 1 Post
Default Solution

I figured out how to make it work:

Code:
public void Delete(Dinner dinner)
{
    foreach(var rsvp in dinner.RSVPs)
    {
        entities.DeleteObject(rsvp);
    }
    entities.DeleteObject(dinner);
}
The Following User Says Thank You to Ole F For This Useful Post:
firedrawndagger (October 12th, 2010)
  #4 (permalink)  
Old October 12th, 2010, 10:50 AM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try:
public void Add(Dinner dinner)
{
entities.AddToDinner(dinner);
}
public void Delete(Dinner dinner)
{
foreach (var rsvp in dinner.RSVP)
{
entities.DeleteObject(dinner.RSVP);
entities.DeleteObject(dinner);
}
  #5 (permalink)  
Old October 12th, 2010, 04:54 PM
Authorized User
 
Join Date: Sep 2010
Posts: 13
Thanks: 1
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by kermit1965 View Post
Try:
public void Add(Dinner dinner)
{
entities.AddToDinner(dinner);
}
public void Delete(Dinner dinner)
{
foreach (var rsvp in dinner.RSVP)
{
entities.DeleteObject(dinner.RSVP);
entities.DeleteObject(dinner);
}
kermit1965, I guess you will get a problem with your code - especially if there are two (or more) RSVP's to a dinner!

Why? Because in you "foreach" loop you first delete the first rsvp. Then you (within your loop) delete the dinner. I guess your application will crash when going to the next RSVP (or maybe the other RSVP's will just stay in the database undeleted...)
  #6 (permalink)  
Old October 12th, 2010, 06:06 PM
Registered User
 
Join Date: Oct 2008
Location: , , .
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry I was not precise. The code was only creating three methods. I did not intend that they be run sequentially.
  #7 (permalink)  
Old October 13th, 2010, 05:50 AM
Authorized User
 
Join Date: Sep 2010
Posts: 13
Thanks: 1
Thanked 1 Time in 1 Post
Default Sorry for not being precise either

Quote:
Originally Posted by kermit1965 View Post
Sorry I was not precise. The code was only creating three methods. I did not intend that they be run sequentially.
I was refering to your Delete Method only.

Imagine:
You have one dinner with dinnerID = 1

You have two RSVP's with dinnerID = 1 and RSVPID = 1 and 2.

In your foreach loop the following will happen:

First loop:
It finds RSVP with RSVPID 1 and deletes it (OK)
It then tries to delete the dinner, but will fail with an exception error as there is a connection to RSVP where RSVPID = 2.

Therefore:
Take the deletion of the dinner outside and after the foreach loop.

Hope this clarifies.
  #8 (permalink)  
Old November 16th, 2010, 03:56 PM
Registered User
 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Ole F View Post
I figured out how to make it work:

Code:
public void Delete(Dinner dinner)
{
    foreach(var rsvp in dinner.RSVPs)
    {
        entities.DeleteObject(rsvp);
    }
    entities.DeleteObject(dinner);
}
This doesn't seem to work. At least not for me. I got it to work by making a copy of the RSVP collection and iterating through the copy. Try this:

Code:
public void Delete(Dinner dinner) {
    List<RSVP> tempRsvps = dinner.RSVPs.ToList<RSVP>();
    foreach (var rsvp in tempRsvps) {
        entities.DeleteObject(rsvp);
    }
    entities.DeleteObject(dinner);
}
  #9 (permalink)  
Old November 16th, 2010, 04:19 PM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

That's very strange. So far I haven't seen anyone have to do that. I would suggest walking through the creation of your entity and verify (especially the foreign key relationship stuff).
  #10 (permalink)  
Old March 10th, 2011, 06:30 AM
Registered User
 
Join Date: Mar 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi I have a question, the methods of the DinnerRepository
to create and delete dinners

What is the right way?

entities.Dinners.AddObject(dinner)

or

entities.AddObject(dinner)

in my case, I am following the book doing the same thing with another example.

if I put the first compiler gives me error, the method AddObject don´t exist for Dinners, in the second, no compiler errors but don´t save.

Help me please. I like the book but I stand by this motif
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Error on page 38 in DinnerRepository adimauro BOOK: Professional ASP.NET MVC 2 3 July 6th, 2010 07:46 PM
Difficulty with $_SESSION mall9@hotmail.com Beginning PHP 11 March 11th, 2006 05:41 PM
Javascript Confirm dialog difficulty ayrton Javascript 1 May 20th, 2005 03:34 AM
ASCII conversion loop difficulty crmpicco Excel VBA 2 May 3rd, 2005 01:41 PM
Difficulty getting thePhile up and running tkubaska BOOK: ASP.NET Website Programming Problem-Design-Solution 3 April 5th, 2004 12:32 PM



All times are GMT -4. The time now is 05:43 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.