p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Professional ASP.NET MVC 2 (http://p2p.wrox.com/forumdisplay.php?f=588)
-   -   Difficulty in DinnerRepository class (http://p2p.wrox.com/showthread.php?t=80995)

dpuranik September 21st, 2010 02:12 AM

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...

Ole F September 23rd, 2010 09:48 AM

I'm listening....
 
as I have the same problem :-(

Ole F September 24th, 2010 06:15 AM

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);
}


kermit1965 October 12th, 2010 10:50 AM

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);
}

Ole F October 12th, 2010 04:54 PM

Quote:

Originally Posted by kermit1965 (Post 264280)
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...)

kermit1965 October 12th, 2010 06:06 PM

Sorry I was not precise. The code was only creating three methods. I did not intend that they be run sequentially.

Ole F October 13th, 2010 05:50 AM

Sorry for not being precise either
 
Quote:

Originally Posted by kermit1965 (Post 264295)
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.

johnhu November 16th, 2010 03:56 PM

Quote:

Originally Posted by Ole F (Post 263677)
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);
}


flyinhawaiian November 16th, 2010 04:19 PM

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).

jlaracena March 10th, 2011 06:30 AM

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


All times are GMT -4. The time now is 11:09 PM.

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