代码之家  ›  专栏  ›  技术社区  ›  Ronnie Overby

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

  •  1
  • Ronnie Overby  · 技术社区  · 15 年前

    在我创建的这个局部方法中,我想更新一些其他对象。我似乎无法将更新持久化到数据库中。这是我删除预订的部分方法。

    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);
    }
    
    2 回复  |  直到 15 年前
        1
  •  5
  •   Marc Gravell    15 年前

    我想这里的问题是它已经打电话了 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);
        }
    }
    
        2
  •  0
  •   Community Mike Kinghan    7 年前