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);
}
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346

2 Answers2

5

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 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 Feb 27 '09 at 15:51
  • 1
    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 Feb 27 '09 at 15:53
  • If that isn't enough... hmmm... don't know. – Marc Gravell Feb 27 '09 at 15:54
  • 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 Feb 27 '09 at 16:08
  • 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 Feb 27 '09 at 16:11
0

Here is an explanation and nice way to update objects in partial classes: Implementing linqtosql partial DataContext class - how to inspect before/after values

I hope it helps.

Community
  • 1
  • 1
Hakan KOSE
  • 751
  • 9
  • 16