views:

1239

answers:

1

I have created an extensibility method for deleting one of my Linq To Sql objects called Reservation.

Well, in this partial method I created, I want to update some other objects. I can't seem to get the update to be persisted in the database. Here is my partial method for deleting the reservation.

public partial class LawEnforcementDataContext
{

    partial void DeleteReservation(Reservation instance)
    {
        // Get ID's of those seated in course
        var roster = new Roster(this, instance.CourseID);
        var seated = from r in roster.All
                     where r.WaitingList == false
                     select r.ID;

        // delete the reservation
        this.ExecuteDynamicDelete(instance);

        // get seated id's not in original seated ids
        var newlySeated = from r in roster.All
                          where r.WaitingList == false && !seated.Contains(r.ID)
                          select r.ID;

        var reservations = this.Reservations.Where(r => newlySeated.Contains(r.ID));

        foreach (var r in reservations)
        {
            r.Confirmed = false;
            // Have tried doing nothing, thinking calling code's db.SubmitChanges() would do the trick
            //this.ExecuteDynamicUpdate(r); HAVE TRIED THIS
        }
        //this.SubmitChanges(); HAVE TRIED THIS
    }
}

The delete is taking place but the update is not. Commented in the last few lines are some of the things I have tried.

Any ideas? Thanks!

EDIT

Here is what I have done to solve this:

   public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{
    ChangeSet delta = GetChangeSet();

    foreach (var res in delta.Deletes.OfType<Reservation>())
    {
        // Get ID's of those seated in course
        var roster = new Roster(this, res.CourseID);
        var seated = from r in roster.All
                     where r.WaitingList == false
                     select r.ID;

        base.SubmitChanges(failureMode);

        // get seated id's not in original seated ids
        var newlySeated = from r in roster.All
                          where r.WaitingList == false && !seated.Contains(r.ID)
                          select r.ID;

        var reservations = this.Reservations.Where(r => newlySeated.Contains(r.ID));

        foreach (var r in reservations)
        {
            r.Confirmed = false;
        }
    }

    base.SubmitChanges(failureMode);
}
+3  A: 

I expect the problem here is that it has already called GetChangeSet().

I suggest you override SubmitChanges() at the data-context, and apply this logic there instead...

partial class LawEnforcementDataContext
{
    public override void SubmitChanges(
        System.Data.Linq.ConflictMode failureMode)
    {
        ChangeSet delta = GetChangeSet();
        foreach (var reservation in delta.Deletes.OfType<Reservation>())
        {
            // etc
        }
        base.SubmitChanges(failureMode);
    }
}
Marc Gravell
I need the delete of the reservation to take place in the foreach loop. Will I have to call GetChangeSet() again after I do the delete?
Ronnie Overby
If you just logically delete it, it should get deleted when the base.SubmitChanges() is called; basically, SubmitChanges calls GetChangeSet internally, and it should now have some extra deletions to do...
Marc Gravell
If that isn't enough... hmmm... don't know.
Marc Gravell
You saved me twice today! Thanks! Look at my edit and see if I am doing this right. You say I don't have to call SubmitChanges twice? It's working this way.
Ronnie Overby
I'm not sure I would have called base.SubmitChanges inside the loop; my intent was to prepare things for deletion in SubmitChanges, and then have it all go at once...
Marc Gravell