我创建了一个可扩展性方法,用于删除一个名为Reservation的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);
}
答案 0 :(得分:5)
我希望这里的问题是它已经调用了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);
}
}
答案 1 :(得分:0)
以下是更新部分类中对象的解释和好方法: Implementing linqtosql partial DataContext class - how to inspect before/after values
我希望它有所帮助。