LINQ to SQL - Updating Data Context Objects in Par

2020-07-29 22:44发布

问题:

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

回答1:

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


回答2:

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.