小编典典

LINQ to SQL-更新局部类中的数据上下文对象

sql

我创建了一个可扩展性方法,用于删除我的Linq To Sql对象之一,称为保留。

好吧,在我创建的这种局部方法中,我想更新其他一些对象。我似乎无法使更新保留在数据库中。这是我删除预订的部分方法。

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

正在进行删除,但没有进行更新。在最后几行中评论了我尝试过的一些事情。

有任何想法吗?谢谢!

编辑

这是我为解决此问题所做的工作:

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

阅读 160

收藏
2021-04-22

共1个答案

小编典典

我希望这里的问题是它已经调用了GetChangeSet()

我建议您SubmitChanges()在数据上下文中覆盖,然后在此处应用此逻辑…

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);
    }
}
2021-04-22